Thread: ios::binary

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    ios::binary

    what is the usage of binary mode in fileio,,,does it make the storeage of data in files easier to organise, if so how do you input strings into a buffer while in binary mode, see below

    Code:
    cout<<"please enter write a sentence > ";
    // cin>> buffer[100]; doesn't work properly it only puts the first   
    
    //word in the buffer
    //and cin.getline doesn't work in binary mode?
    //what can you use or do to get around this
    cheers
    WhAtHA hell Is GoInG ON

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >how do you input strings into a buffer while in binary mode
    The same way you do in text mode. The only difference is that text conversions aren't made, so if the file has a CR LF pair that's normally converted to '\n' in text mode, it won't be in binary mode and you can get the CR and LF individually. Despite how it seems, you can use text input functions in binary mode.

    Cheers!

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    In addition, on platforms where there is no difference beteween text and binary files (Unix/Linux for example), ios::binary is unneccessary.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >// cin>> buffer[100];
    Assuming buffer is declared as:
    Code:
    char buffer[100]
    the above should be:
    Code:
    cin >> buffer;
    getline would be:
    Code:
    cin.getline(buffer, sizeof(buffer));

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    on platforms where there is no difference beteween text and binary files (Unix/Linux for example), ios::binary is unneccessary
    But it's still recommended. Otherwise when good, useful code is ported to another system--you do write good, useful code, right? --forgetting to add the binary orientation could cause some frustratingly subtle errors. On Linux, for example ios::binary is basically a no-op, but on Windows it makes a big difference, so to be comfortably portable, you specify it on both even though you don't have to on Linux unless you're lazy or antisocial.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    what would you recommend using with file io, binary or text mode, i need to organise data in a file i thought binary was better for this usage,

    and yes i'm still new to programming and i thankyou for the help
    WhAtHA hell Is GoInG ON

  7. #7
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >what would you recommend using with file io, binary or text mode
    Text is easier to work with and more portable, so it's usually what I use. But since you're learning, work with both to get a feel for how things work.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    <off topic>And is there something wrong with being lazy and antisocial? I *am* a programmer!</off topic>

  9. #9
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    <off topic>And is there something wrong with being lazy and antisocial? I *am* a programmer!</off topic>
    Well, Linux people are always trying to convert non-Linux people, so being anti-social is against the cult creed, I think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 03:01 AM
  2. Trouble with I/O
    By Dae in forum C++ Programming
    Replies: 15
    Last Post: 07-03-2005, 09:10 PM
  3. fstream confusion
    By alphaoide in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2003, 10:02 AM
  4. ios::binary.... a discussion (mostly)
    By Betazep in forum C++ Programming
    Replies: 19
    Last Post: 04-17-2002, 11:03 AM
  5. Reading/Writing in Binary
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 10:32 PM