Mega Code Archive

 
Categories / Delphi / Examples
 

Blockread

The following code shows how to use a untyped input file to blockread a text file, scan the input buffer for any character, and replace it with a carriage return and line feed. Since it is using 16k input and output buffers the speed is quite reasonable. NOTE: If when you are through, if the line length of the output file exceeds 255 characters and you want to read it using ReadLn just use more than one string in the ReadLn statement to as follows: ReadLn(infile,string1,string2); This will read up to a 510 char line with the 1st 255 char in string1 and the remainder in string2; program fixfile;{ Compile from the DOS prompt: DCC FIXFILE.PAS } uses { execute from File Manager } sysutils,dialogs,forms; type bufptr = obufr; iobufr = array[0..16384] of char; var infile : file; oufile : textfile; inbufr, oubufr : bufptr; idx: integer; bytesread: integer; bytes2read: integer; totalbytesread: longint; actualfilesize: longint; OpenDialog1: TOpenDialog; infilename,oufilename: string; begin infilename := ''; OpenDialog1 := TOpenDialog.Create(Application); OpenDialog1.Options := []; OpenDialog1.Filter := 'All files|*.*'; OpenDialog1.FilterIndex := 1; OpenDialog1.Title := 'Enter Source file name to be converted'; if OpenDialog1.execute then infilename := OpenDialog1.filename; if infilename='' then begin OpenDialog1.free; halt; end; OpenDialog1.Title := 'Enter Destination file name to be created'; if OpenDialog1.execute then oufilename := OpenDialog1.filename; OpenDialog1.free; if oufilename='' then halt; if infilename=oufilename then halt; new(inbufr); new(oubufr); assignfile(infile,infilename); reset(infile,1); actualfilesize := filesize(infile); assignfile(oufile,oufilename); system.settextbuf(oufile,oubufr^); rewrite(oufile); totalbytesread := 0; bytesread := 0; bytes2read := 0; while (totalbytesread < actualfilesize) and (bytes2read=bytesread) and (IOresult=0) do begin if (actualfilesize-totalbytesread)>sizeof(inbufr^) then bytes2read := sizeof(inbufr^) else bytes2read := actualfilesize-totalbytesread; blockread(infile,inbufr^,bytes2read,bytesread); totalbytesread := totalbytesread + bytesread; for idx := 0 to bytesread do if inbufr^ [idx]='''' then { <= character to be converted } writeln(oufile) else write(oufile,inbufr^ [idx]); end; closefile(infile); closefile(oufile); dispose(inbufr); dispose(oubufr); end.