Click here to Skip to main content
15,886,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How is fread supposed to behave when used on a file opened in Text Mode. Is it meant to condense the \r\n pairs into single \n's or does it read the file verbatim.

Furthermore, if I open a File in Text Mode for writing. Will fwrite always expand an \n into \r\n.

Footnote:
The \n, \r\n issue still permeates through the software industry. It was I think in hindsight a disasterous decission by Microsoft to go for \r\n. It was not in their culture, they did not do this when they wrote Basic for the Commodore 64. Incidentily, at the time they had also a concept of Dollar Terminated Strings. Maybe someone felt at the time, that real programers use\r\n instead of \n. We are to this day living with the consequences!

:-D
Posted
Updated 19-Jul-10 14:11pm
v2
Comments
Christian Graus 19-Jul-10 20:12pm    
I would say that given that fread is only used in C, that Microsoft probably doesn't care much anymore. I'd seek the answer to these questions by writing some code and seeing what happens.
Bram van Kampen 19-Jul-10 20:31pm    
No, fread is very much a standard library function,available both in 'C' and 'CPP' and must be supported by Microsoft, if they claim (as they do) that their compiler and API's comply with relevant ISO Standards. The standard stipulates that fread should be equivalent to a number of fgetch()calls. This would Translate things automatically. My version of the MS Library does not seem to support that.

Why would nobody use fread and fwrite in CPP. I use it daily in CPP and MFC Programs.
Christian Graus 19-Jul-10 20:42pm    
I don't see why any intelligent person would use any C API in C++, because iostreams is just so much better. The only reason they exist in CPP is that CPP was built on C to encourage people who used C to learn it. I never once used C file APIs in all the years I wrote CPP code. Visual Studio is increasingly standards compliant, so I'd assume it complies with the standard.

You said:

The \n, \r\n issue still permeates through the software industry. It was I think in hindsight a disasterous decission by Microsoft to go for \r\n.


But this is not so true: it is not a Microsoft decision! Please, have a look to the links below:

Newline - Wikipedia, the free encyclopedia[^]
Line Breaks in Windows, UNIX and Macintosh Text Files[^]

The \r\n representation for newline was already in use before that Microsoft was founded! And not only: Macintosh systems prior to MacOS X used \r instead than \r\n and \n!
 
Share this answer
 
When you read a file in text mode the C90 standard (as implemented by Microsoft's compilers) says the implementation can do what it likes when it reads and write the file.

This means all bets are off if you try and use fread with a file opened in text mode. Want to read that unixy file with 0xDA sequences in on Windows? Use a binary file.

As Sauro mentioned Macs on OS9 and earlier translated '\r' to '\n' when they read and people managed to write portable code. The general rule is if you open a file in text mode use things like fprintf and fscanf for formatted input and output. If you open it in binary mode use fread and fwrite as the raw bytes won't be scrambled.

Cheers,

Ash
 
Share this answer
 
Comments
Bram van Kampen 20-Jul-10 20:31pm    
Well, it confirms my suspicions that all bets are off. So, I'm going to do it by Hand! fread the file in bin mode, Copy it with all '/r/n'contracted to single '/n', process it, and send it out again with each '/n' replaced with a '/r/n.
(Sorry Slashes run the Wrong Way, The CP Editor deletes the Correct Slashes)


It comes back though to what I said earlier, I never accused Microsoft of inventing the concept. At the time they did not have the foresight of perpetuating a problem. If MS had not adopted the termination, it would have been assigned to history a long time ago, along with things like the Dollar Terminated String, invented by Microsoft, which was famous in MS Dos 2. Thanks and Regards, Bram
Aescleal 21-Jul-10 14:53pm    
Another historical note: The dollar terminated string was actually specified in the CPM interface. No idea who thought of it before Gary Kildall though.

Ash
It's my understanding that fread doesn't care about file mode - text vs binary. If you look here[^] you'll see that fread's behaviour is defined in terms of multiple calls to fgetc, which in turn makes no mention of end of line mappings, just a stream of bytes (with a comment that bytes and characters aren't necessarily the same).

I think a similar thing applies to fwrite.

Footnote:
I'm old enough to remember when teletypes screwed up if you didn't use \r\n, or even \r\n\0xff\0xff\0xff on some machines. (The carriage returned slowly.) To me, Unix/C using just \n was the aberration, and things haven't improved in the subsequent 30-some years, as you observe.
 
Share this answer
 
SQL
When answering a question please:

Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.


Well, You dismissed my question, and deleted your own answer, + my Previous Comment.
Should the above not apply to yourself too?

Regards,
 
Share this answer
 
Comments
Christian Graus 19-Jul-10 21:13pm    
There are no deleted comments or answers on this thread that I can see. Perhaps there was a server glitch ?
VB
It's my understanding that fread doesn't care about file mode - text vs binary. If you look here[^] you'll see that fread's behaviour is defined in terms of multiple calls to fgetc, which in turn makes no mention of end of line mappings, just a stream of bytes (with a comment that bytes and characters aren't necessarily the same).


Well, That's essentially the problem. The comittee that wrote the ISO Standard, essentially took a step back from this issue. P.J Plaugher described the issue quite sustinctly in his book: 'The Standard 'C' Library', Quote '...And when the CRT Terminal arrived, everybody took a step back... Should the End of a Text Line be indicated by a Line Feed, a carriage Return, a Combination of Both, or by some New incantation..."
"... The comitee could not reach agreement on this, and decided to leave this to the judgement of the individual implementors..."

This all may seem quite irrellevant for writing windows, it does become relevant if the app in question spews out HTML Files.
 
Share this answer
 
Comments
Peter_in_2780 19-Jul-10 21:21pm    
I don't see that "that's the problem". fread's behaviour is well-defined, even if it's not the answer to a maiden's prayer. So the answer to your original question (second sentence) is "the latter - verbatim".
btw, who was your previous answer aimed at?
C++
btw, who was your previous answer aimed a

Apologies.
Due to a failure somewhere I got a familiar email to click 'here', with no further contents.

When I clicked 'Here' all I found was Grause's message which was at the centre of the page and nothing else. I responded to apparently Christian Grause deleting my message!
I Don't know if this is a server or Browser issue.

fread's behaviour is well defined in that it is decided that it should not be defined in the first place. So, I guess, I should Start with a test during Initialisation about how the OS behaves, and act accordingly.

Regards,

Bram.
 
Share this answer
 
CSS
Do not hit 'reply' to this email: To view, click here.

Christian Graus has posted an comment to your Answer "fread behaviour":

There are no deleted comments or answers on this thread that I can see. Perhaps there was a server glitch ?

--------------------------------------------------------------------------------

Note: This message has been sent from an unattended email box.


Well There seems to be a Glitch Somewhere.

Regards,

Bram
 
Share this answer
 
No,Absolutely Not!!

io Streams are better only from the point where you start from. i.e.: You Save to an io stream, you Retrieve from an io stream in the same paradigm. That will work, as long as everybody uses the same compiler. I save and retrieve data according to a binary spec. The Next module that picks up a record may have been written in Fortran,Algol, Pascal, or any other language. It is the way that data is laid out in binary format that unites all languages in this case.
 
Share this answer
 
Comments
Christian Graus 20-Jul-10 21:08pm    
iostreams do work best when you are reading and writing, and can create your own code to stream an object in one go, that much is true. However, the comment 'I use fread in MFC apps every day' led me to assume that you were one of the people who learned C first and never learned C++ properly, holding on instead to outdated concepts that travelled over from C. If that's not the case, I apologise, but that seems to me to be the norm, probably in part because of university professors who have made the same error, and gone on to teach it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900