Board index » delphi » Reading a file with ctrl Z before end of file

Reading a file with ctrl Z before end of file

Hello all,
Is there anyway to read a file that has a control z  before the end of
file. I have a file created by a redirection of a dos command.  In that
file is the end of file mark but it comes before the actual eof.  I have
tried to declare the file as file of char thinking I could keep on
reading, but it only reads till the control z which I know is the eof
marker. I think the way to do it is a for loop but I can't figure out
the loop structure.  Can any one help or did I make my problem clear as
mud?
Thanks,
HowarD

 

Re:Reading a file with ctrl Z before end of file


Quote
TorchIT wrote:

> Hello all,
> Is there anyway to read a file that has a control z  before the end of
> file. I have a file created by a redirection of a dos command.  In that
> file is the end of file mark but it comes before the actual eof.  I have
> tried to declare the file as file of char thinking I could keep on
> reading, but it only reads till the control z which I know is the eof
> marker. I think the way to do it is a for loop but I can't figure out
> the loop structure.  Can any one help or did I make my problem clear as
> mud?
> Thanks,
> HowarD

Hmm... I'm not sure, but instead of using the EOF function, can't you keep reading, and keep track of the
bytes, until you reached the end, using the lenght_of_file function (i know it's not the proper name, but.. :)
I'm not sure how the lenght_of_file function works, so... And perhaps the read procedure doesn't like reading
^Z. I don't know, give it a try.

- Asbj?rn

Re:Reading a file with ctrl Z before end of file


Quote
> Is there anyway to read a file that has a control z  before the end of
> file. I have a file created by a redirection of a dos command.  In that
> file is the end of file mark but it comes before the actual eof.  I have
> tried to declare the file as file of char thinking I could keep on
> reading, but it only reads till the control z which I know is the eof
> marker. I think the way to do it is a for loop but I can't figure out
> the loop structure.  Can any one help or did I make my problem clear as
> mud?

Howard,
     This is, in my opinion, an Implementation Bug (Feature?) of
Borland's Pascal dialect.  The file type "text" should treat ^Z as EOF
if that's what your operating system suggests, as this file type is
meant to be implementation-specific, and tailored to the concept of
lines and (logical) EOF.
     The file type "char" should, on the other hand, treat the entire
character set, which is also somewhat implementation-specific, but is
probably either 7-bit Ascii, 8-bit Ascii, or EBCDIC; since we're talking
PC-DOS, it's probably 8-bit Ascii, and ^Z is just another Ascii
character.
     However, it looks like Borland is giving this character special
significance, and using it as EOF.  [Incidently, you might wonder if it
treats ^@, 00H, the null character, as a real character, or whether it
simply skips over it].  If so, you cannot use FILE OF char to solve your
problem.
     How about trying PACKED FILE OF bytetype, where bytetype is defined
as the restricted integer range -128 .. 127?  You may have to experiment
a bit, as it is again somewhat implementation-dependent whether this
will work (an alternative declaration that might work if this doesn't
is 0 .. 255).  Now ^Z is simply the number 26.  You can easily convert
these bytes back into characters using the chr() function (though you
may need to be careful if you get 8-bit Ascii values and have defined
bytetype as -128..127).

Bob Schor
Pascal Enthusiast

Re:Reading a file with ctrl Z before end of file


Quote
Bob Schor <bsc...@vms.cis.pitt.edu> wrote:
>     However, it looks like Borland is giving this character special
>significance, and using it as EOF.  

Would probably agree with you...

Glenn Grotzinger
Web Page: http://www.geocities.com/Paris/3537
Writer of Excellent Training Manual known as the TP Tutorial.
You may find this material on the web page eventually.
Other interesting things will eventually exist as well.

Re:Reading a file with ctrl Z before end of file


Quote
TorchIT wrote:
> Hello all,
> Is there anyway to read a file that has a control z  before the end of
> file. I have a file created by a redirection of a dos command.  In that
> file is the end of file mark but it comes before the actual eof.  I have
> tried to declare the file as file of char thinking I could keep on
> reading, but it only reads till the control z which I know is the eof
> marker. I think the way to do it is a for loop but I can't figure out
> the loop structure.  Can any one help or did I make my problem clear as
> mud?

No, not that hard to understand.  Rather than worry about it in code, I
usually just use this little program as a sort of filter to strip
problematic text files.

program stripEOFs;

{
 uneof.pas -- written by Scott Earnest (set...@ix.netcom.com)

 Quickly removes all offending EOF (ASCII #26) characters from a
 text file by changing them to ASCII #0.  This method will fail on
 read-only files, and is more  of a kludge than a good solution.

 Standard disclaimers apply.

Quote
}

type
  tblock = array[0 .. 32767] of byte;

var
  dblock : tblock;
  x : word;
  f : file;
  count : word;
  fp : longint;

begin
  if paramcount <> 1 then
    begin
      writeln ('Specify a file to strip EOF''s from!');
      halt;
    end;
  assign (f, paramstr(1));
  reset (f,1);
  fp := 0;
  while not eof(f) do
    begin
      fp := filepos(f);
      blockread (f, dblock, sizeof (tblock), count);
      for x := 0 to pred(count) do
        if dblock[x] = $1a then dblock[x] := $00;
      seek (f,fp);
      blockwrite (f, dblock, count);
    end;
  close (f);
end.

Quote
> Thanks,
> HowarD

--
Scott Earnest                      | _,-""-_,-""-_,-""-_,-""-_,-""-_,-"
|
set...@ix.netcom.com (primary)     | We now return you to our regularly
|
siny...@{*word*104}space.org (alternate) | scheduled chaos and mayhem. . . .
|

Re:Reading a file with ctrl Z before end of file


You can convert your handle to a binary file handle, see the source of the
printer unit on the bonus disk.
Second, look for the filesize first and then read as many bytes as the
filesize is.

--
Greetings,
ASS-Ware.

           ,,,
          (o o)
 -----oOO--(_)--OOo-----
|       ASS-Ware        |
|    is watching you    |
 -----------------------

TorchIT <howl...@mail.airmail.net> wrote in article
<59ms2u$...@library.airnews.net>...

Quote
> Hello all,
> Is there anyway to read a file that has a control z  before the end of
> file. I have a file created by a redirection of a dos command.  In that
> file is the end of file mark but it comes before the actual eof.  I have
> tried to declare the file as file of char thinking I could keep on
> reading, but it only reads till the control z which I know is the eof
> marker. I think the way to do it is a for loop but I can't figure out
> the loop structure.  Can any one help or did I make my problem clear as
> mud?
> Thanks,
> HowarD

Re:Reading a file with ctrl Z before end of file


Quote
Bob Schor <bsc...@vms.cis.pitt.edu> writes:
> > Is there anyway to read a file that has a control z  before the end of
> > file. I have a file created by a redirection of a dos command.  In that
> > file is the end of file mark but it comes before the actual eof.  I have
> > tried to declare the file as file of char thinking I could keep on
> > reading, but it only reads till the control z which I know is the eof
> > marker.
>      This is, in my opinion, an Implementation Bug (Feature?) of
> Borland's Pascal dialect. [...]
>      The file type "char" should, on the other hand, treat the entire
> character set [...]
>      However, it looks like Borland is giving this character special
> significance, and using it as EOF.

Not as far as I can see. Maybe it was different with earlier versions, but I
just tested with BP 7.0, and the file of char read properly behind the #26.

For text files: They do stop at the #26, that's how they're designed. If you
wnat to change this behaviour, take a look at:
http://www.mi.uni-erlangen.de/~heckenb/programs.htm#CrLf
This contains a program to convert unix style line breaks to Dos style. It can
be adapted to remove EOFs (the code will get even easier).

Hope this helps,
Frank

Other Threads