Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   I met a wierd thing when using fwrite() and fputc(), 0x0d is automatically added before 0x0a (http://www.velocityreviews.com/forums/t459817-i-met-a-wierd-thing-when-using-fwrite-and-fputc-0x0d-is-automatically-added-before-0x0a.html)

Vivienne 01-14-2007 02:14 PM

I met a wierd thing when using fwrite() and fputc(), 0x0d is automatically added before 0x0a
 
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?


Vaclav Haisman 01-14-2007 02:27 PM

Re: I met a wierd thing when using fwrite() and fputc(), 0x0d isautomatically added before 0x0a
 
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

Paul 01-14-2007 02:46 PM

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

"Vivienne" <zhoudan.bupt@gmail.com> wrote in message
news:1168784090.539589.197780@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



Vivienne 01-15-2007 02:20 AM

Re: I met a wierd thing when using fwrite() and fputc(), 0x0d is automatically added before 0x0a
 
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" <zhoudan.bupt@gmail.com> wrote in message
> news:1168784090.539589.197780@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




All times are GMT. The time now is 05:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.