Mike
If you are not concerned too much with speed, you can read
it a character at a time as a File of Byte, and if you get
an EOF before reading Filesize characters, ignore it.
Something like this...
Var Fin, Fout : File of Byte;
FSize : Integer;
FRead : Integer;
B : Byte;
Begin
AssignFile(Fin, 'TEXT.TXT');
FSize := FileSize(Fin);
Reset(Fin,1);
AssignFile(Fout, 'TEXT2.TXT');
Rewrite(Fout);
FRead := 0;
While FRead < FSize Do
Begin
Read(Fin, B);
If B <> EOF then
Write(Fout, B);
Inc(FRead);
End;
CloseFile(Fout);
CloseFile(Fin);
End;
This will be fairly slow, and is untested. To speed it up
for large files, allocate a buffer (maybe an array of 1K or
so) and read a buffer full at a time, then search the
buffer, removing the EOF's, and then writing the buffer back
out.
Jeff.
EOF in a text file *means* "stop here". If you are reading a file in
text mode then you should stop at the first EOF.
If the file contains the EOF character somewhere that is not supposed
to be the end of the file then it isn't a text file and you shouldn't
be reading it as if it were <s>.
Use BlockRead() instead of Read().
Alternative use TStreamFile..
Andrue Cope
[Bicester, UK]
Thanks for the speedy reply! I'll give that a shot. The text files are
small so it should be pretty quick.
Mike
>When trying to read in a text file, I am encountering a premature EOF
>marker. Is there a way that I can strip these eof markers out of the
>file?? Maybe by reading each character and then writing them back out
>to another file but skipping over the eof somehow.
>
>Mike
>
Thanks
Gene Felch
Dublin, NH
j...@monad.net
Thanks for the reply!
Mike
Besides the null problem, text files also read eof on ^Z (#26).
PhR
"Jeffrey A. Wormsley" wrote:
Ok, because I have the same problem as Mike, I tried to use your solution.
But I've got some errors :
- At the line Reset(Fin,1) - > too many parameters
- At the line If B <> EOF then -> Incompatible types
I think B is a Byte an EOF returns FALSE or TRUE so this can't work ?
Do you have a solution for this ?
Please reply me as soon as possible. THANK YOU !
Greetings from Luxemburg/Europe.
- At the line Reset(Fin,1) - > too many parameters
- At the line If B <> EOF then -> Incompatible types
I think B is a Byte an EOF returns FALSE or TRUE so this can't work ?
Do you have a solution for this ?
Please reply me as soon as possible. THANK YOU !
>>
The first error is because Jeff used a command for untyped files, with a
file of byte. All you need is reset(Fin). The second uses an unfortunate
constant name for what I think is 0. Just use if b<>0.
I'm going from memory here, because the previous messages are too old for my
offline reader settings. If the problem is to remove all #0 from a text
file, the following will be many times faster (and simpler).
--------------
Procedure removeNulls(sSrc, sDest: string);
(*Pre: sSrc is an existing file, sDest a valid filename.
Post: sDest is a copy of sSrc minus all 0 bytes.*)
Var
fSrc, fDest: file;
s: string;
j0, j: integer;
Begin
assign(fSrc, sSrc);
reset(fSrc, 1);
try
setLength(s, filesize(fSrc));
blockRead(fSrc, s[1], length(s));
except
close(fSrc);
end;
assign(fDest, sDest);
rewrite(fDest, 1);
try
j0 := 1;
for j := 1 to length(s) + 1 do begin
//length + 1 is #0
if (s[j] = #0) then begin
blockWrite(fDest, s[j0], j-j0);
j0 := j + 1;
end;
end;
except
close(fDest);
end;
End;
---------------
This is untested. If your file is larger than 100M, say, then the procedure
must be modified to load only partial blocks, instead of the whole file at
once. Ask again if that is problem. Likewise, the procedure can be modified
to remove other chars besides #0. Delphi will not read a text file beyond
^Z, for instance.
PhR