Thread: ostream

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    ostream

    "ofstream::<<" looks much easier to use then "ofstream::write".

    Any concerns that we should rather use "ofstream::write"?

    Maybe buffer overflows and such?

    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    
    ofstream file("test.txt" ); 
    
    int main()
    {
        string f,i;
        f = "Hello\n";
        i = " world\n";
    
        file.write("Hello",  5);
        file.write(i.c_str(),7);
    
        file << "Hello" << i;
    
        return 0;
    }
    PS There is a typo in the title meant to be ofstream, Sorry.
    Last edited by Ducky; 09-09-2009 at 08:52 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You should use write when you want to write the bytes of something, rather than the representation that operator<< is coded for.

    E.g

    Code:
    #include <iostream>
    
    int main()
    {
        int i = 4407873;
        std::cout.write(reinterpret_cast<char*>(&i), sizeof(i));
    }
    This might print "ABC", except it isn't very portable due to endianness or what-not.

    Or more generally, it can be used to output byte arrays that do not represent C-style null-terminated strings and may therefore contain embedded '\0'-s. Note that it has a second parameters specifying the size of the char array. operator<< outputs only up to the first null-character it encounters.
    Last edited by anon; 09-09-2009 at 09:02 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Anon!

    So if i understand well "ofstream::write" should be used when writing a binary file.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose so. Binary file IO doesn't translate '\n' into platform-specific end-of-line sequence which would corrupt data where '\n' does not have that meaning (when you output the bytes of an integer, it might well contain bytes with the value of '\n').
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istream and ostream not declared?
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 01:02 AM
  2. ostream valid flag?
    By AH_Tze in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2004, 03:29 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. ostream and istream
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2003, 07:36 AM
  5. extending ostream
    By ilear_01 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2003, 11:27 PM