Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

c++ IO streams or C FILE ???

DantyDanty Member Posts: 322
are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:

[code]
std::fstream fs(std::ios_base::in | std::ios_base::out);
fs << "hello world";
// [red] now i want to delete (purge) the entire file contents[/red]
// sure i can close/reopen and truncate....but dont' want to have to close file
[/code]

Comments

  • DonotaloDonotalo Member Posts: 715
    [b][red]This message was edited by Donotalo at 2006-5-14 22:44:20[/red][/b][hr]
    : are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
    : whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:
    :
    : [code]
    : std::fstream fs(std::ios_base::in | std::ios_base::out);
    : fs << "hello world";
    : // [red] now i want to delete (purge) the entire file contents[/red]
    : // sure i can close/reopen and truncate....but dont' want to have to close file
    : [/code]
    :
    [purple]C++ io fstreams can be used for binary io to file. for that purpose, u need to open the file in binary mode. a sample code:

    [b]fstream fs(ios::binary);[/b]

    now fs is ready for binary input/output. fstreams open in [blue]ios::in | ios::out[/purple] mode by default. so i omitted that part.
    [/purple]
    [hr][purple]~Donotalo()[/purple]



  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
    : whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:
    :
    : [code]
    : std::fstream fs(std::ios_base::in | std::ios_base::out);
    : fs << "hello world";
    : // [red] now i want to delete (purge) the entire file contents[/red]
    : // sure i can close/reopen and truncate....but dont' want to have to close file
    : [/code]
    :
    [blue]FILE* is much simpler than streams and I suspect that streams built on top of good old FILE*, so they slower, obvioiusly.[/blue]
  • Griz803Griz803 Member Posts: 100
    : : are C++ IO streams really used for binary IO? If so, any quick reference to examples (I guess I have to buy the book Standard C++ IOStreams and Locales) ??
    : : whenever I try to use IO streams instead of good old C FILE* (although more work) i run into problems:
    : :
    : : [code]
    : : std::fstream fs(std::ios_base::in | std::ios_base::out);
    : : fs << "hello world";
    : : // [red] now i want to delete (purge) the entire file contents[/red]
    : : // sure i can close/reopen and truncate....but dont' want to have to close file
    : : [/code]
    : :

    [green]Search for seekp and / or tellp. These should be the equivalent of the seek and tell functions from FILE * management. This should let you move the existing file pointer to the beginning of the file, thus truncating without closing. Remember to check error conditions since this can get you massive runtimes if mishandled! I hope this helps. [/green]
  • stoberstober Member Posts: 9,765 ✭✭✭
    you also have to use stream's read() and write() methods, not the >> or << operators.

    How streams are implemented is compiler dependent -- the compiler may, or may not, simply wrap FILE and associated functions. The standards do not dictate how streams are implemented -- but I think the Microsoft compiler do just wrap them with FILE functions, and FILE functions are a wrapper for win32 api functions. I don't know how *nix or MAC implements them.
  • DantyDanty Member Posts: 322
    ok, it seems that all of the above replies have same thoughts as I initially did.

    using the ios_base::binary mode will not do it, it simply interperts "
    ", "
    ", at end of strings.... Even write()/read() will not really do true C's FILE* binary. To really do it I'd have to derive and implement from streambufs class.

    then notion that i'm getting is that C++ IO streams are only for STRINGs.

    check out this site, the best i've found so far (like i said, i think i need to buy that book :) )

    [green]
    see Binary IO section:
    http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html
    [/green]
  • stoberstober Member Posts: 9,765 ✭✭✭
    [b][red]This message was edited by stober at 2006-5-15 17:43:48[/red][/b][hr]
    I don't see the problems with binary i/o that are mentioned in the article you posted. If you use VC++ 2005 express, open a text file in binary mode then read the entire file into one huge buffer. After it is read, use the debugger to view all the bytes in the file -- you will notice that the data in the buffer is an exact copy of the data on the file. ifstream did not change the file one byte.

    If you still don't believe it, compile and run this program with your compiler -- it writes and reads a true binary file, not mearly a text file treated as binary.
    [code]
    #include
    #include
    using namespace std;

    int main()
    {
    long a = 123;
    long b = 456;
    float c = 234.456f;

    ofstream out("myfile.dat",ios::binary);
    out.write((char *)&a,sizeof(long));
    out.write((char *)&b,sizeof(long));
    out.write((char *)&c,sizeof(float));
    out.close();
    a = b = 0;
    c = 0.0;
    ifstream in("myfile.dat", ios::binary);
    in.read((char *)&a,sizeof(long));
    in.read((char *)&b,sizeof(long));
    in.read((char *)&c,sizeof(float));
    in.close();
    cout << "a = " << a << "
    ";
    cout << "b = " << b << "
    ";
    cout << "c = " << c << "
    ";
    return 0;
    }
    [/code]




  • DantyDanty Member Posts: 322
    yeah that works, i guess what i ment to say was that C++ Binary IO mode is not well, standard like a text mode standards. i'd still have to take care of bit ordering, data type bit sizes ... if i were to write binary data files for different platforms.
  • stoberstober Member Posts: 9,765 ✭✭✭
    : yeah that works, i guess what i ment to say was that C++ Binary IO mode is not well, standard like a text mode standards. i'd still have to take care of bit ordering, data type bit sizes ... if i were to write binary data files for different platforms.
    :


    yes and you have to do that with C's FILE functions too. binary files are not often easily transferrable between operating systems.
Sign In or Register to comment.