Originally Posted by
itsme86
*scratches head* If the EOF value was actually stored at the end of the file data, how would you store whatever value EOF is in the file if the OS would interpret it as the end of the file?
You wouldn't. This was used for text files (ascii characters). If you opened a text file and used standard formatted i/o functions (getchar()), for example) there were two ways that you could get an EOF from the function:
1. The character that was read was 0x1a (control-z)
or
2. The file had a length that was exactly an integer multiple of the disk block size and you had already read the last character.
So, text files could not contain 0x1a as a character in the file.
In those days, directory information did not contain the exact byte count of the file contents. Nowadays it does.
Interestingly enough, for Borland bcc32 and Microsoft Visual C++ (version 6.0), files opened as "r" still quit if a 0x1a character is found. Files opened as "rb" do not. For Gnu gcc, 0x1a is not recognized as EOF regardless of how the file is opened.
Dave
Try the following, with the attachment I have enclosed:
Code:
#include <stdio.h>
int main()
{
int inchar;
FILE *infile;
char *inname = "x.txt";
if ((infile = fopen(inname, "rb")) == NULL) {
printf("Can't open input file %s with \"rb\".\n", inname);
return 1;
}
printf("\n\n");
printf("Here's the result when %s was opened with \"rb\":\n", inname);
while ((inchar = fgetc(infile)) != EOF) {
putchar(inchar);
}
printf("\n\n");
fclose(infile);
if ((infile = fopen(inname, "r")) == NULL) {
printf("Can't open input file %s\n", inname);
return 1;
}
printf("Here's the result when %s was opened with \"r\":\n", inname);
while ((inchar = fgetc(infile)) != EOF) {
putchar(inchar);
}
printf("\n\n");
fclose(infile);
return 0;
}
p.s. enter the following in a command line window:
type x.txt
Open x.txt with your favorite text editor.
Dave