Reputation: 4940
Suppose i open a text file in write mode using c language. Now I add some text data to it.
1.Internally how is data stored in file ? Is each character stored as 8 bit ascii code ?
We Never add EOF at the end of writing to file and we use fclose() to close the file .
2.How is then EOF added to file ? How is it stored in file ?
When we read character by character of that file using getchar() , We are able to detect EOF. Now EOF if is ctrl+z , these are 2 characters ^z are saved at end of file. So getchar() will get ^ and then z . so,
3.How does getchar() detects EOF ?
Upvotes: 1
Views: 600
Reputation: 726619
EOF
is not a character that gets stored in a file, it is a special return code that you get when you read a file. The file I/O system knows how many characters there are in a file, because it stores the exact length of the file. When your program tries to read a character after the last available character, the file I/O system returns a special value EOF
, which is outside the range of char
(it is for that reason that character reading routines such as getchar()
return an int
instead of a char
).
The Ctrl+Z sequence is not an EOF
character either. It is a special sequence of keys that tells the shell to close the console input stream associated with the program. Once the stream is closed, the next read returns EOF
to your program. It is important to understand, however, that Ctrl+Z is merely a keyboard sequence that is interpreted by the command line processor - in the same way that Ctrl+C is a sequence that tells the command line processor to terminate the program.
Finally, ^Z
is not two characters that get stored in a file, it's a screen representation of the Ctrl+Z sequence produced by the command line processor to confirm visually that the keyboard sequence has been accepted.
Upvotes: 1
Reputation: 2130
Typically C will be using Latin-1 or some other single byte encoding, but it should be possible to use UTF-8 locale setting. Note that most C character/string handling routines will not properly handle UTF-8 or any other multibyte encoding -- you have to use special libraries.
It depends on the Operating System used, but most will simply store a continuous stream of characters, with a Line-End (CR-LF in Windows, \n in Unixy systems) character to mark the end of the line (YOU have to explicitly put it there).
Some Operating Systems, such as MS-DOS, may explicitly write an EOF character to the end of the file, but most don't. They simply run off the end of the file and report a status of EOF.
See 2.
Upvotes: 1