424,279 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

writing 0x0A to a bin file without having it automatically write 0x0D

P: n/a
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");
fprintf(tmp_ifp, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....

Mar 13 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
MC*******@gmail.com writes:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");
fprintf(tmp_ifp, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?


You need to open the file in "binary" mode, e.g.

tmp_ifp = fopen("tmp_name.bin", "wb");
--

John Devereux
Mar 13 '06 #2

P: n/a
MC*******@gmail.com writes:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");


Use binary mode: "wb" instead of "w".
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Mar 13 '06 #3

P: n/a
On 2006-03-13, MC*******@gmail.com <MC*******@gmail.com> wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");
fprintf(tmp_ifp, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....


I think these routines can convert 0xa to a system defined
line terminator : which I guess in your case is 0xD,0xA.

Check the options which can follow "w" in your fopen : b or t might
help if memory serves correctly. Since you talk about "bin" file then
"wb" might be your man.
Mar 13 '06 #4

P: n/a
On 2006-03-13, MC*******@gmail.com <MC*******@gmail.com> wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");
"wb".
fprintf(tmp_ifp, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....

Mar 13 '06 #5

P: n/a
On 2006-03-13, Richard G. Riley <rg****@gmail.com> wrote:
On 2006-03-13, MC*******@gmail.com <MC*******@gmail.com> wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");
fprintf(tmp_ifp, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....


I think these routines can convert 0xa to a system defined
line terminator : which I guess in your case is 0xD,0xA.

Check the options which can follow "w" in your fopen : b or t might
help if memory serves correctly. Since you talk about "bin" file then
"wb" might be your man.


t is not standard C<OT>, and where it is used, it means text mode</OT>.
Mar 13 '06 #6

P: n/a
On 13 Mar 2006 14:03:44 -0800, MC*******@gmail.com wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name.bin", "w");
fprintf(tmp_ifp, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?


You opened the file in text mode. Therefore, it is not a binary file,
regardless of what the three character extension is. If you want to
process the file in binary mode, open it binary mode.

For additional insurance, don't use fprintf. Use fwrite.
Remove del for email
Mar 14 '06 #7

P: n/a
In article <bd********************************@4ax.com>,
Barry Schwarz <sc******@doezl.net> wrote:
For additional insurance, don't use fprintf. Use fwrite.


Huh? How can these be different?

-- Richard
Mar 14 '06 #8

P: n/a
Thanks....I opened it with "wb" and it worked. I'm surprised that just
using "w" wouldn't. Why would you want to act on the data being
written to a file in that manner? Doesn't make sense. If someone
wanted a new line then they would just put "\n".
Thanks everyone else for your help too!

Mar 14 '06 #9

P: n/a
MikeC_EE99 wrote:
Thanks....I opened it with "wb" and it worked. I'm surprised that just
using "w" wouldn't. Why would you want to act on the data being
written to a file in that manner?
So that you write the correct data.
Doesn't make sense.
Does.
If someone wanted a new line then they would just put "\n".


But in a /file/, on some OSs incl Windows, a new line is represented
as \r\n, not just \n. So, for /text streams/, the C library converts
\r\n on input to \n, and \n on output to \r.

If you don't want that to happen, you're not writing a text stream,
so you have to tell C that you want a "binary" file.

--
Chris "sparqling" Dollin
"Who do you serve, and who do you trust?"
Mar 14 '06 #10

P: n/a
On 2006-03-14, MikeC_EE99 <MC*******@gmail.com> wrote:
Thanks....I opened it with "wb" and it worked. I'm surprised that just
using "w" wouldn't. Why would you want to act on the data being
written to a file in that manner? Doesn't make sense. If someone
wanted a new line then they would just put "\n".
Thanks everyone else for your help too!


Different OSs terminate text lines with different combinations."b"
tells the routine to put what you want, not what the file "convention"
is on that system
Mar 14 '06 #11

P: n/a
On 14 Mar 2006 14:04:28 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
In article <bd********************************@4ax.com>,
Barry Schwarz <sc******@doezl.net> wrote:
For additional insurance, don't use fprintf. Use fwrite.


Huh? How can these be different?

fprintf is going to "massage" the format string, making the
"appropriate" substitutions for every conversion specification. As a
Windows system specific implementation detail, this massaging also
appears to include converting \n to \n\r (or is it \r\n). That this
happens on text files is obvious. I don't know whether it also
happens on binary files but fwrite definitely performs no such
massaging. Hence the phrase about insurance.
Remove del for email
Mar 14 '06 #12

P: n/a
In article <ui********************************@4ax.com>,
Barry Schwarz <sc******@doezl.net> wrote:
fprintf is going to "massage" the format string, making the
"appropriate" substitutions for every conversion specification. As a
Windows system specific implementation detail, this massaging also
appears to include converting \n to \n\r (or is it \r\n). That this
happens on text files is obvious. I don't know whether it also
happens on binary files but fwrite definitely performs no such
massaging. Hence the phrase about insurance.


I don't have a Windows machine to test it on, but I can see nothing in
the standard to justify this behaviour. The conversion of characters
is controlled by the stream type, which is in turn controlled by the
"b" flag to fopen(), not by the choice of fprintf() or fwrite().

-- Richard
Mar 14 '06 #13

P: n/a
On 2006-03-14, Barry Schwarz <sc******@doezl.net> wrote:
On 14 Mar 2006 14:04:28 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
In article <bd********************************@4ax.com>,
Barry Schwarz <sc******@doezl.net> wrote:
For additional insurance, don't use fprintf. Use fwrite.
Huh? How can these be different?

fprintf is going to "massage" the format string, making the
"appropriate" substitutions for every conversion specification. As a
Windows system specific implementation detail, this massaging also
appears to include converting \n to \n\r (or is it \r\n).


It is not printf that does this. It is putc, which is called [or as if
it is called] by every other output function. \n is not a conversion
specification.
That this happens on text files is obvious. I don't know whether it
also happens on binary files but fwrite definitely performs no such
massaging.
It is self-evident that it does, on text files. An implementation where
the output caused by fwrite is not as if putc were called in a loop is
non-conforming.
Hence the phrase about insurance.

Mar 14 '06 #14

P: n/a
On 2006-03-14, MikeC_EE99 <MC*******@gmail.com> wrote:
Thanks....I opened it with "wb" and it worked. I'm surprised that just
using "w" wouldn't. Why would you want to act on the data being
written to a file in that manner?
Compatibility with text files used by other programs on the platform,
some of which may not be written in C.
Doesn't make sense. If someone wanted a new line then they would just
put "\n". Thanks everyone else for your help too!

Mar 14 '06 #15

P: n/a
Barry Schwarz <sc******@doezl.net> writes:
On 14 Mar 2006 14:04:28 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
In article <bd********************************@4ax.com>,
Barry Schwarz <sc******@doezl.net> wrote:
For additional insurance, don't use fprintf. Use fwrite.


Huh? How can these be different?

fprintf is going to "massage" the format string, making the
"appropriate" substitutions for every conversion specification. As a
Windows system specific implementation detail, this massaging also
appears to include converting \n to \n\r (or is it \r\n). That this
happens on text files is obvious. I don't know whether it also
happens on binary files but fwrite definitely performs no such
massaging. Hence the phrase about insurance.


#if USE_FWRITE
fwrite("hello, world\n", 1, strlen("hello, world\n"), stream);
#else
fprintf(stream, "hello, world\n");
#endif

If the data written to "stream" differs depending on whether
USE_FWRITE is defined, then the implementation of either fwrite() of
fprintf() is broken. Both are equivalent to a sequence of calls to
fputc(). Both will translate '\n' to a system-specific end-of-line
representation if stream is opened in text mode; neither will do so if
stream is opened in binary mode.

--
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.
Mar 14 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.