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

V

Vivienne

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?
 
V

Vaclav Haisman

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.
 
P

Paul

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-lite/how-to-post.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
 
V

Vivienne

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 дµÀ£º
"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top