IO of binary files

Discussion in 'C' started by pradeep, Mar 20, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    To make sure that there is no CR/LF translation on non-Unix computers, you have to use the following lines to open streams to files with binary data.

    Code:
    ofstream os("output.flt", ios::out | ios::binary);
     ifstream is("output.flt", ios::in | ios::binary);
     
    For Visual C++, when using fstream.h, use in addition the flag ios::nocreate. Otherwise you can open a non-existing file for reading, without complaining. (This is not necessary when using fstream).
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    One should not use the .h versions of header files in C++ when there is a non-h version available. They were provided originally for backward compatibility. If you use, for instance, stdio.h, rather than cstdio, you will not get the protection afforded by the std namespace.

    Text mode in windows machines also does more than add the CR; it adds a soft EOF. Some append modes do not deal properly with it. Since all files are binary, anyway, it's best not to use text mode unless you know for a fact that a file you are going to read was written in text mode.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice