Question:
Why do i have to specify file type (binary or text ) in C?
Sirius
2013-05-03 11:17:39 UTC
I read some article that said in text files the M.Os are always 0.
I opened a file in text mode and wrote a byte with the value 128 to it using fwrite.. and read from it using fread and i got the same value?
Have i got it wrong?
please recommend an article or a book along with your answer.
Thank you.
Three answers:
jplatt39
2013-05-03 11:20:35 UTC
In essence you have to specify in Windoze. In UNIX and Linux we don't handle files the same way and don't care.
Laurence I
2013-05-03 18:37:46 UTC
there is more to it than that



when you open a file as a text file, that is using a certain ROUTE through the I/O system

characters include CONTROL characters, these affect what happens to a TEXT output channel.

things like all do things. This is because the FLOW is treated like it were sent to a CONSOLE. Data may not be written to the actual file until an End Of Line is output.



to a Binary file the data is of no interest and is NOT watched and does not affect FLOW.

to a text file this is not the case.



you can write Unprintable character codes to a TEXT file, but ODD things might happen if you tried to Read a line of text with that code in it.

Nul characters might cause nasty effects.

other codes might produce End of file effects.
roger
2013-05-03 23:29:29 UTC
The major difference is how the end of line character is handled.

In a binary type file no changes are made to the file data.

int a text file the newline character '\n' is translated to '\n''\r' (line feed /carriage return)and back when saving.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...