fstream::write and '\n' and '\r'

This is a discussion on fstream::write and '\n' and '\r' within the C++ Programming forums, part of the General Programming Boards category; Code: fstream ifs(name, ios::out | ios::trunc | ios::binary); unsigned __int8 word[2]; word[0]=(unsigned __int8)((unsigned __int16)width >> 8); word[1]=(unsigned __int8)width; if(!ifs.write((char *)word, ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    12

    fstream::write and '\n' and '\r'

    Code:
    fstream ifs(name, ios::out | ios::trunc | ios::binary);
    unsigned __int8 word[2];
    
    word[0]=(unsigned __int8)((unsigned __int16)width >> 8);
    word[1]=(unsigned __int8)width;
    if(!ifs.write((char *)word, 2))
    	throw new fileio_error("Cannot write width to file.");
    Ignoring the other funkyness, this snippet should be writing {0x00, 0x0A} to the file, but is instead writing {0x00, 0x0D, 0x0A}. My immediate thought is that Windows is being retarded and adding carriage returns to all newlines written, which in this case is incorrect behavior, but I had thought that opening the file in binary mode would fix this problem. What do I need to do to have the snippet write exactly what I want, no questions asked, to the file?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    it would most certainly help to know the value of width

    print out width as an int16 - my guess is it's 218. if it is, then it's probably not microsoft.

    microsft won't add a 0x0D in the code that you posted, however, say you read a file, as binary, that you created in notepad. in notepad you typed "AB", hit the enter key, then typed "CD". if you echo the contents to the screen in hex, you would get

    0x41 0x42 0x0D 0x0A 0x44 0x45 End-of-file

    edit: and depending on what source you get width from, you may get the same thing from the console. if you read stdin via a binary read, then the chances are you picked up the microsoft standard 0x0D 0x0A newline delimiter
    Last edited by misplaced; 04-04-2005 at 12:22 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12
    Width is most definitely 10.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    one thing you might want to try is
    Code:
    ...
    ifs.write((unsigned __int8 *)word, 2);
    char is not guaranteed to be exactly 8 bits.

    edit: excuse me, i mean

    Code:
    ...
    ifs.write((char *)word, (2 * float(  sizeof(_int8) / sizeof(char)   ))   );
    edit again: if nothing else, try

    Code:
    fstream ifs(name, ios::out | ios::trunc | ios::binary);
    char word[2];
    
    cout << sizeof(char) << endl;
    word[0]= 0;
    word[1]= 10;
    if(!ifs.write((char *)word, 2))
    	throw new fileio_error("Cannot write width to file.");
    hopefully size of char is 1. that WILL write 0x00, 0x0A

    if size of char is 2 then:
    Code:
    fstream ifs(name, ios::out | ios::trunc | ios::binary);
    char word[2];
    
    word[0] = 10;
    
    if(!ifs.write((char *)word, 1))
    	throw new fileio_error("Cannot write width to file.");
    Last edited by misplaced; 04-04-2005 at 01:27 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by DonFiasco
    Code:
    fstream ifs(name, ios::out | ios::trunc | ios::binary);
    ...
    if(!ifs.write((char *)word, 2))
    Ignoring the other funkyness, this snippet should be writing {0x00, 0x0A} to the file, but is instead writing {0x00, 0x0D, 0x0A}. My immediate thought is that Windows is being retarded and adding carriage returns to all newlines written, which in this case is incorrect behavior, but I had thought that opening the file in binary mode would fix this problem. What do I need to do to have the snippet write exactly what I want, no questions asked, to the file?
    If you have opened the file as std::ios::binary and the second argument of the ofstream write() member function is 2, it should write exactly two bytes. (No questions asked.) If there are more than two bytes, then either:

    1. It was not opened as std::ios::binary

    or

    2. Something or someone else wrote something.

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21