Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EOF in text file

489 views
Skip to first unread message

Mike

unread,
Jul 28, 1999, 3:00:00 AM7/28/99
to
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


Jeffrey A. Wormsley

unread,
Jul 28, 1999, 3:00:00 AM7/28/99
to

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.

Andrue Cope

unread,
Jul 28, 1999, 3:00:00 AM7/28/99
to
Mike,

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]


Mike

unread,
Jul 28, 1999, 3:00:00 AM7/28/99
to
Jeff,

Thanks for the speedy reply! I'll give that a shot. The text files are
small so it should be pretty quick.

Mike

Gene Felch

unread,
Jul 28, 1999, 3:00:00 AM7/28/99
to
On Wed, 28 Jul 1999 07:40:04 -0400, Mike <mi...@5starserv.com> wrote:
You might try using an untyped file and BlockRead ing the data, the
parsing it into s tStringList.

>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

Mike Wasilewski

unread,
Jul 29, 1999, 3:00:00 AM7/29/99
to
Actually, using the code that Jeffery posted, I read the file a character
at a time and found that the problematic characters were nulls( ascii 0
). I then used the same code to strip those chars and all is fine.

Thanks for the reply!

Mike

Philippe Ranger

unread,
Aug 1, 1999, 3:00:00 AM8/1/99
to
<<Mike:

When trying to read in a text file, I am encountering a premature EOF
marker.
>>

Besides the null problem, text files also read eof on ^Z (#26).

PhR

Robert HOFFMANN

unread,
Aug 25, 1999, 3:00:00 AM8/25/99
to

"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.

Philippe Ranger

unread,
Aug 25, 1999, 3:00:00 AM8/25/99
to
<<Robert to Jeff:

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 !
>>

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

0 new messages