•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 430,118 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,338 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 632 | Replies: 7
![]() |
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation:
Rep Power: 1
Solved Threads: 37
What's the difference exactly between opening a file stream in the default text mode and opening it in binary mode? The cplusplus.com article on file i/o mentioned a few things, but I really didn't get the gist of it. Could someone explain it?
Thanks.
Thanks.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
A text stream will invoke translation of the system defined newline marker to a \n (on reading), and the reverse \n to system defined on writing.
A binary stream doesn't do this.
There is perhaps an implicit assumption that a file opened in text mode might actually consist only of printable characters and cursor control characters.
A binary stream doesn't do this.
There is perhaps an implicit assumption that a file opened in text mode might actually consist only of printable characters and cursor control characters.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,251
Reputation:
Rep Power: 38
Solved Threads: 940
The difference also depends on the operating system -- on *nix there is no difference between text and binary written files because *nix compilers make no translation of the '\n' character in either mode. That is, the file itself will contain the same information whether writting with a stream opened in text or binary mode.
That is not true for files writting by MS-DOS, MS-Windows, and MAC. I don't know about other operating systems such as RISC but I suppose it might be true. On MS-DOS/MS-Windows when a file is opened in text mode the '\n' is translated to "\r\n" when written to the file. MAC it is translated to '\r'. So when read back into the program the "\r\n" (or '\r') is transated back to just '\n'.
That is not true for files writting by MS-DOS, MS-Windows, and MAC. I don't know about other operating systems such as RISC but I suppose it might be true. On MS-DOS/MS-Windows when a file is opened in text mode the '\n' is translated to "\r\n" when written to the file. MAC it is translated to '\r'. So when read back into the program the "\r\n" (or '\r') is transated back to just '\n'.
<<Freelance Programmer>> << Hobby Site>>
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation:
Rep Power: 1
Solved Threads: 37
•
•
•
•
from my experience you open up files like mp3's in binary, so that you can read any type of info (not just text) at precise position in files. Text mode is for reading/writing text.
That's what I assumed as well, but writing (and I'm assuming reading) from binary file streams uses chars to transmit the data, so you're still using text. Not that you can't type cast the char to become the type of data you want it to be, or type-cast an different data type to a char to be output.
After all, a hard drive doesn't see chars, or ints, or doubles - it just sees bits. So it really doesn't matter what we output the data as in C++. Once it gets to the hard drive, it becomes bits, and then any program can interpret the data in the file any way it wants.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,251
Reputation:
Rep Power: 38
Solved Threads: 940
•
•
•
•
After all, a hard drive doesn't see chars, or ints, or doubles - it just sees bits. So it really doesn't matter what we output the data as in C++. Once it gets to the hard drive, it becomes bits, and then any program can interpret the data in the file any way it wants.
True, but that doesn't mean the program will interpret the data correctly. Example: move a file written in text mode on MS-Windows to *nix, then use a *nix text editor to read it. Or do the opposite -- move a text file writting on *nix to MS-Windows and use Notepad.exe to read it. In both cases the editor will mis-interpret the line feeds. In some cases the text editor may be smart enough to translate the file correctly, but most simple text editors such as Notepad.exe can not do that.
Last edited by Ancient Dragon : Jul 8th, 2008 at 8:32 pm.
<<Freelance Programmer>> << Hobby Site>>
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation:
Rep Power: 1
Solved Threads: 37
•
•
•
•
True, but that doesn't mean the program will interpret the data correctly. Example: move a file written in text mode on MS-Windows to *nix, then use a *nix text editor to read it. Or do the opposite -- move a text file writting on *nix to MS-Windows and use Notepad.exe to read it. In both cases the editor will mis-interpret the line feeds. In some cases the text editor may be smart enough to translate the file correctly, but most simple text editors such as Notepad.exe can not do that.
Ya I know. I was just saying that at first I assumed that a binary file stream would output with something other than chars, but then I realized that the fact that you output with chars disappears as soon as the file reaches the hard drive. It's all bytes by then.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Porting VB -> C++
- Next Thread: problem in accessing methods from VC++



)
Linear Mode