What is wrong?

David Goodenough dg at lakart.UUCP
Thu Jan 26 07:44:10 AEST 1989


>From article <2051 at ndsuvax.UUCP>, by numork at ndsuvax.UUCP (James Mork):
>   I am using TC 1.5 everything was going OK... Now... when I run
>   a simple program like this, the file gets truncated after 25 or
>   26 bytes...    After 25 when I read the file back in, I just
>   get EOF (-1).  What is wrong?  Is this just some error that
>   I am staring at, or is my compiler in foo bar land?

The infamous MS-DOS (and CP/M) end of file problem. 26 is the magic number.
Early DOS programs and ALL CP/M programs that deal with text files do so in
complete blocks: 128 bytes at a time. So how do you deal with a text file
20 characters long? Easy: put the 20 characters in, and fill the rest of
the block with 0x1a. This means that anyone reading the file _IN TEXT MODE_
will see the first 0x1a as an end of file. In particular your putc(26, fp);
will put an EOF in a text file. NOW try doing the same with the files open
in binary mode (r+ or rb or whatever it is), and you may have somewhat
better luck.
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.lang.c mailing list