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
2008-12-22 16:23:06
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
2008-12-22 17:15:13
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
2008-12-22 18:15:04
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
2008-12-22 17:02:39
+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
2008-12-23 02:51:53
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
2008-12-23 08:57:05
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
2008-12-25 02:52:33
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
2008-12-27 21:37:14