2009. május 16., szombat

Convert a UNIX linefeed delimited text file to a DOS CR/LF delimited file


Problem/Question/Abstract:

I'm having an issue converting a Unix 1.15 GB text file to a Windows file. It always seems my application runs out of memory during the conversion. Does anyone have any ideas as to how to accomplish this?

Answer:

It sounds like you're trying to read the whole file into memory or something. How does this work:

program unix2dos;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  fp1: file;
  fp2: TextFile;
  buffer, buf2: array[1..8192] of Char;
  numread: Integer;
  i: Integer;
begin
  if paramcount <> 2 then
  begin
    writeln('USAGE : UNIX2DOS <input file> <output file>');
    writeln(' Takes UNIX text file (linefeed delimited) and');
    writeln(' converts it to a DOS (CR/LF) delimited text file.');
    halt(10);
  end;
  if FileExists(Paramstr(1)) then
  begin
    AssignFile(fp1, paramstr(1));
    Reset(fp1, 1);
    AssignFile(fp2, paramstr(2));
    SetTextBuf(fp2, buf2);
    rewrite(fp2);
    repeat
      BlockRead(fp1, buffer, sizeof(buffer), Numread);
      if Numread <> 0 then
      begin
        for i := 1 to Numread do
        begin
          if buffer[i] = #10 then
            writeln(fp2)
          else
            write(fp2, buffer[i]);
        end;
      end;
    until
      NumRead = 0;
    close(fp1);
    close(fp2);
  end
  else
    writeln('Could not find file : ', paramstr(1));
end.

Nincsenek megjegyzések:

Megjegyzés küldése