By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

I met a wierd thing when using fwrite() and fputc(), 0x0d is automatically added before 0x0a

P: n/a
I am using VS 2005.

In a project when I was trying to write some unsigned char into a file,
I used fwrite() and fputc(). But I found whenever I tried to write
0x0a, fwrite() function automaticly wrote 0x0d before 0x0a. fputc()
gives the same results. So I wrote a small test to test it. Here is it:
FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;
fwrite(*p,1,1,file);

offset = ftell(file); // after this, the offset = 2. and
in the file I

// found 0x0d and 0x0a was
written.

I can't understand. I just want to write byte by byte, automatically
inserted byte 0x0d will cause problems in my project.

can someone tell me what is the cause and how to avoid the unwanted
byte?

Jan 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Vivienne wrote, On 14.1.2007 15:14:
I am using VS 2005.

In a project when I was trying to write some unsigned char into a file,
I used fwrite() and fputc(). But I found whenever I tried to write
0x0a, fwrite() function automaticly wrote 0x0d before 0x0a. fputc()
gives the same results. So I wrote a small test to test it. Here is it:
FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;
fwrite(*p,1,1,file);

offset = ftell(file); // after this, the offset = 2. and
in the file I

// found 0x0d and 0x0a was
written.

I can't understand. I just want to write byte by byte, automatically
inserted byte 0x0d will cause problems in my project.

can someone tell me what is the cause and how to avoid the unwanted
byte?
You are opening your file in text mode, and you are on Windows. Make sure you
open your files with "b" mode if you do not want the semantics of text streams.

--
VH
Jan 14 '07 #2

P: n/a

"Vivienne" <zh**********@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Here is it:
>

FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;
If this is the code, you are accessing an uninitialized pointer "file".
Nowhere do you set "file" to point somewhere.

Read the FAQ on posting these type of questions:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

So, is this your real code, or did you open the file somehow? If you did
open the file, did you open it as binary or text? If you opened it as text,
that is more than likely the reason for your problem.

Read up on fopen( ) and the difference in opening a file as binary or text.
The hint is the second parameter to fopen.

Paul
Jan 14 '07 #3

P: n/a
This is not the real code, I just copied some lines here.
now I have got the answer, I should have opened the file in banery
mode, instead text mode.
Thank you very much!

"Paul д
"
"Vivienne" <zh**********@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Here is it:


FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;

If this is the code, you are accessing an uninitialized pointer "file".
Nowhere do you set "file" to point somewhere.

Read the FAQ on posting these type of questions:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

So, is this your real code, or did you open the file somehow? If you did
open the file, did you open it as binary or text? If you opened it as text,
that is more than likely the reason for your problem.

Read up on fopen( ) and the difference in opening a file as binary or text.
The hint is the second parameter to fopen.

Paul
Jan 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.