Why we should use binary mode of file? Where it is going to be
actually needed??? 10 2624
In article <11**********************@j4g2000prf.googlegroups. com>,
Shraddha <sh*************@gmail.comwrote:
>Why we should use binary mode of file? Where it is going to be actually needed???
MS Windows. Text files there have their lines delimited by
carriage return + linefeed pairs, the pair together reading in
as a single \n in text mode. If you used text mode to try to
write out a file of binary data, and happened to write out
a linefeed (pretty likely by chance in a file of any significant
size) then instead of having just the linefeed output, both
carriage return and linefeed would be output (on that particular
platform.) That's going to ruin the file in many circumstances.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Shraddha said:
Why we should use binary mode of file?
If you mean "why should we open a file in binary mode?", the answer is
"because we want to access the file as a raw stream of bytes, rather
than as a newline-delimited text file."
Where it is going to be actually needed???
When you're opening a file that isn't an ordinary text file, or even
when you want to access an ordinary text file as if it weren't one.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On Thu, 21 Jun 2007 10:30:15 -0700, in comp.lang.c , Shraddha
<sh*************@gmail.comwrote:
>Why we should use binary mode of file? Where it is going to be actually needed???
If youre storing or reading binary data, use binary mode.
any OSen translate certain characters when tehy're written in text
mode - such as the end-of-line marker, the end-of-file marker etc.
So if you wrote a JPEG image out using text mode, the data would be
garbled. Ditto, in reverse, when you're reading it back in again.
By the way, windows isn't the only OS to do this, and not all OSes do
it the same way - MacOS, MSDOS, VMS (I think) probably CP/M and
probably TOS and AmigaDos all seem to do it, though not in the same
way.
Myself, I think Unix has it wrong - to return to the next line on my
teletype, I definitely need a CR and an LF.....
--
Mark McIntyre
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
On Thu, 21 Jun 2007 18:50:03 +0100, Mark McIntyre
<ma**********@spamcop.netwrote:
>On Thu, 21 Jun 2007 10:30:15 -0700, in comp.lang.c , Shraddha <sh*************@gmail.comwrote:
>>Why we should use binary mode of file? Where it is going to be actually needed???
If youre storing or reading binary data, use binary mode.
[snip]
>Myself, I think Unix has it wrong - to return to the next line on my teletype, I definitely need a CR and an LF.....
And definitely in that order...
Bill (nostalgic, but way OT)
--
William D Waddington wi****************@beezmo.com
"Even bugs...are unexpected signposts on
the long road of creativity..." - Ken Burtch
On Jun 21, 2:22 pm, Bill Waddington <william.wadding...@beezmo.com>
wrote:
On Thu, 21 Jun 2007 18:50:03 +0100, Mark McIntyre
<markmcint...@spamcop.netwrote:
On Thu, 21 Jun 2007 10:30:15 -0700, in comp.lang.c , Shraddha
<shraddhajosh...@gmail.comwrote:
>Why we should use binary mode of file? Where it is going to be actually needed???
If youre storing or reading binary data, use binary mode.
[snip]
Myself, I think Unix has it wrong - to return to the next line on my
teletype, I definitely need a CR and an LF.....
And definitely in that order...
Possibly padded with up to three trailling ASCII NUL (0x00) bytes
Mark McIntyre wrote On 06/21/07 13:50,:
On Thu, 21 Jun 2007 10:30:15 -0700, in comp.lang.c , Shraddha
<sh*************@gmail.comwrote:
>>Why we should use binary mode of file? Where it is going to be actually needed???
If youre storing or reading binary data, use binary mode.
any OSen translate certain characters when tehy're written in text
mode - such as the end-of-line marker, the end-of-file marker etc.
So if you wrote a JPEG image out using text mode, the data would be
garbled. Ditto, in reverse, when you're reading it back in again.
By the way, windows isn't the only OS to do this, and not all OSes do
it the same way - MacOS, MSDOS, VMS (I think) probably CP/M and
probably TOS and AmigaDos all seem to do it, though not in the same
way.
Myself, I think Unix has it wrong - to return to the next line on my
teletype, I definitely need a CR and an LF.....
<topicality level=marginal>
So, you're saying the end-of-line convention for Unix
should be CR LF NUL NUL NUL? Or maybe a varying number of
NULs depending on the length of the line and the speed of
the modem?
</topicality>
-- Er*********@sun.com
On Jun 21, 3:15 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Mark McIntyre wrote On 06/21/07 13:50,:
On Thu, 21 Jun 2007 10:30:15 -0700, in comp.lang.c , Shraddha
<shraddhajosh...@gmail.comwrote:
>Why we should use binary mode of file? Where it is going to be actually needed???
If youre storing or reading binary data, use binary mode.
any OSen translate certain characters when tehy're written in text
mode - such as the end-of-line marker, the end-of-file marker etc.
So if you wrote a JPEG image out using text mode, the data would be
garbled. Ditto, in reverse, when you're reading it back in again.
By the way, windows isn't the only OS to do this, and not all OSes do
it the same way - MacOS, MSDOS, VMS (I think) probably CP/M and
probably TOS and AmigaDos all seem to do it, though not in the same
way.
Myself, I think Unix has it wrong - to return to the next line on my
teletype, I definitely need a CR and an LF.....
<topicality level=marginal>
So, you're saying the end-of-line convention for Unix
should be CR LF NUL NUL NUL? Or maybe a varying number of
NULs depending on the length of the line and the speed of
the modem?
Long ago, at the lowest level (output from line conditioning and
serial driver, for instance), it was often necessary to add padding
characters after the CR LF to compensate for very slow mechanical
parts. IIRC, Unix did this sort of thing circa Version 3 or earlier.
Nowadays, it is likely very unnecessary to do such padding
(BTW, I was just responding to Bill's "nostalgia" remark :-) )
</topicality>
--
Eric.Sos...@sun.com
On Jun 21, 3:20 pm, Lew Pitcher <lpitc...@teksavvy.comwrote:
On Jun 21, 3:15 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Mark McIntyre wrote On 06/21/07 13:50,:
On Thu, 21 Jun 2007 10:30:15 -0700, in comp.lang.c , Shraddha
<shraddhajosh...@gmail.comwrote:
>>Why we should use binary mode of file? Where it is going to be
>>actually needed???
If youre storing or reading binary data, use binary mode.
any OSen translate certain characters when tehy're written in text
mode - such as the end-of-line marker, the end-of-file marker etc.
So if you wrote a JPEG image out using text mode, the data would be
garbled. Ditto, in reverse, when you're reading it back in again.
By the way, windows isn't the only OS to do this, and not all OSes do
it the same way - MacOS, MSDOS, VMS (I think) probably CP/M and
probably TOS and AmigaDos all seem to do it, though not in the same
way.
Myself, I think Unix has it wrong - to return to the next line on my
teletype, I definitely need a CR and an LF.....
<topicality level=marginal>
So, you're saying the end-of-line convention for Unix
should be CR LF NUL NUL NUL? Or maybe a varying number of
NULs depending on the length of the line and the speed of
the modem?
Long ago, at the lowest level (output from line conditioning and
serial driver, for instance), it was often necessary to add padding
characters after the CR LF to compensate for very slow mechanical
parts. IIRC, Unix did this sort of thing circa Version 3 or earlier.
Nowadays, it is likely very unnecessary to do such padding
(BTW, I was just responding to Bill's "nostalgia" remark :-) )
<topicality level="way off topic">
FWIW, Unix character device (terminal) support still provides this,
sort of, with the OFILL termios() option.
</topicality>
</topicality>
--
Eric.Sos...@sun.com
On Thu, 21 Jun 2007 12:20:44 -0700, in comp.lang.c , Lew Pitcher
<lp******@teksavvy.comwrote:
(Eric wrote)
> So, you're saying the end-of-line convention for Unix should be CR LF NUL NUL NUL? Or maybe a varying number of NULs depending on the length of the line and the speed of the modem?
Long ago, at the lowest level (output from line conditioning and serial driver, for instance), it was often necessary to add padding characters after the CR LF to compensate for very slow mechanical parts. IIRC, Unix did this sort of thing circa Version 3 or earlier.
ISTR the chain drive on the printer in my lab used to jump off if you
sent it a CR/LF and then a printable character immediately, so NULs
were definitely required. Apparently a mechanical constraint about how
fast the chain could pull the drum round etc...
--
Mark McIntyre
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Shraddha <sh*************@gmail.comwrites:
Why we should use binary mode of file? Where it is going to be
actually needed???
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 12 covers
stdio; question 12.40 addresses your question.
Please check the FAQ before posting questions here; that's what it's
for.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tron Thomas |
last post by:
What does binary mode for an ofstream object do anyway? Despite which
mode the stream uses, operator << writes numeric value as their ASCII
representation.
I read on the Internet that it is...
|
by: Steven T. Hatton |
last post by:
§27.4.2.1.4 Type ios_base::openmode
Says this about the std::ios::binary openmode flag:
*binary*: perform input and output in binary mode (as opposed to text mode)
And that is basically _all_ it...
|
by: John R. Delaney |
last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write...
|
by: joelagnel |
last post by:
hi friends,
i've been having this confusion for about a year, i want to know the
exact difference between text and binary files.
using the fwrite function in c, i wrote 2 bytes of integers in...
|
by: vim |
last post by:
hello everybody
Plz tell the differance between binary file and ascii
file...............
Thanks
in advance
vim
|
by: smith4894 |
last post by:
Hello all,
I'm working on writing my own streambuf classes (to use in my custom
ostream/isteam classes that will handle reading/writing data to a
mmap'd file).
When reading from the mmap...
|
by: masood.iqbal |
last post by:
Hi,
Kindly excuse my novice question. In all the literature on ifstream
that I have seen, nowhere have I read what happens if you try to read
a binary file using the ">>" operator. I ran into...
|
by: Hatzigiannakis Nikos |
last post by:
I have written the following code in DEV C++ but the produced file is a
normal text file (you can type it in command line). I was expecting that the
numeric values would have written in their...
|
by: rory |
last post by:
I can't seem to append a string to the end of a binary file. I'm using
the following code:
fstream outFile("test.exe", ios::in | ios::out | ios::binary |
ios::ate | ios::app)...
|
by: Erwin Moller |
last post by:
Why is a binary file executable? Is any binary file executable? Is
only binary file executable? Are all executable files binary? What is
the connection between the attribute of binary and that of...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |