views:

1185

answers:

4

I have a text file with dos elements - hex(00) for exampel. I need to read it and convert it. Have tried to use utf8toansi, but this removes the whole line with hex(00). Are there any way to convert to text in the whole file ? I am using win32, RadStudio2007, Delphi.

+2  A: 

You could read it into a TMemoryStream, assign a PChar to the first address, and scan with a loop from 0 to Size - 1. It's up to you of course what you want to do with the embedded #0 chars. Just ignore the #13 and add a string on each #10 that you encounter.

EDIT:

Something like:

procedure TForm1.Button1Click(Sender: TObject);
var
  FileStream: TStream;
  MemStream: TMemoryStream;
  P: PChar;
  s: string;
  i: integer;
begin
  FileStream := TFileStream.Create('file.txt', fmOpenRead);
  try
    MemStream := TMemoryStream.Create;
    try
      MemStream.LoadFromStream(FileStream);
      P := PChar(MemStream.Memory);
      s := '';
      for i := 0 to MemStream.Size - 1 do begin
        case P[0] of
          #0: {};
          #10: begin
                 // do something with s
                 s := '';
               end;
          #13: {};
        else
          s := s + P[0];
        end;
        Inc(P);
      end;
    finally
      MemStream.Free;
    end;
  finally
    FileStream.Free;
  end;
end;
mghie
TMemoryStream has a LoadFromFile method, obviating the intermediate TFileStream. Also, note that building up the string one character at a time is a bad idea. Store the start position and the length, and then use SetString.
Rob Kennedy
I put this together in Delphi 4, where there is no TFileStream.LoadFromFile(), but this is of course a very good idea for later Delphi versions. Re 2) - it's a very good comment, and I would never do this in production code, I'd use two PChars. But I did not want to overcomplicate the example...
mghie
A: 

I don't know Delphi, but my guess is you probably need to open the file as a binary file and not a text file. Then you can read the bytes directly and convert them to whatever you want.

dviljoen
+3  A: 

This should do...

procedure ConvertFileToDos(const aInFile,aOutFile:String);
var
  FileIn,FileOut:TextFile;
  C:AnsiChar;
  LineBreak:String;
begin
  LineBreak := #13#10;

  AssignFile(FileIn,aInFile);
  Reset(FileIn);

  AssignFile(FileOut,aOutFile);
  ReWrite(FileOut);

  while not EOF(FileIn) do
  begin
    Read(FileIn,C);
    if C=#0 then
      Write(FileOut,LineBreak)
    else
      Write(FileOut,C)
  end;

  CloseFile(FileIn);
  CloseFile(FileOut);
end;
Wouter van Nifterick
A: 

Thank you very much for answering :) I tried the last solution ( ConvertFileToDos) and it seems to be working nicely for me :)

Sorry, but I would never have guessed from the question that you wanted to convert a text file to a different format. Why using Delphi at all in that case? How about standard tools like tr, sed, awk?
mghie
He mentions that he's on win32, so the mentioned tools are probably not available. Besides that, I can't think of anything that those tools can do that Delphi can't.
Wouter van Nifterick
Tools are available, using MSys, Cygwin or GnuWin32. And even if Delphi can build similar tools - do you also build your hammer and pliers yourself? How does that saying go: "Those who do not understand Unix are condemned to reinvent it, poorly." (Henry Spencer)
mghie