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

Discussion in 'C++' started by Vivienne, Jan 14, 2007.

  1. Vivienne

    Vivienne Guest

    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?
     
    Vivienne, Jan 14, 2007
    #1
    1. Advertising

  2. 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
     
    Vaclav Haisman, Jan 14, 2007
    #2
    1. Advertising

  3. Vivienne

    Paul Guest

    "Vivienne" <> wrote in message
    news:...
    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
     
    Paul, Jan 14, 2007
    #3
  4. Vivienne

    Vivienne Guest

    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" <> wrote in message
    > news:...
    > 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
     
    Vivienne, Jan 15, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Gael GRICOURT

    Tricky cout with 0x0A value

    Jean-Gael GRICOURT, Feb 1, 2004, in forum: C++
    Replies:
    6
    Views:
    3,074
    Jean-Gael GRICOURT
    Feb 2, 2004
  2. cesare
    Replies:
    1
    Views:
    341
    Dennis Lee Bieber
    Mar 31, 2006
  3. Replies:
    15
    Views:
    1,115
    Keith Thompson
    Mar 14, 2006
  4. Arian Sanusi
    Replies:
    0
    Views:
    468
    Arian Sanusi
    Jan 27, 2008
  5. David Garamond
    Replies:
    21
    Views:
    283
    Gergely Kontra
    Jun 1, 2004
Loading...

Share This Page