Thread: Writing text data to a binary file with fsteam

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Writing text data to a binary file with fsteam

    fstream ifs;
    ifs.open ("data.str", ios::binary | ios:ut);

    char *data1 = "data1";
    char *data2 = "data2";

    ifs.write(data1, strlen(data1));
    ifs.write(data2, strlen(data2));

    when i this,data2 is not going under data1, i thought each write starts on a new line?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just because it's a binary file doesn't mean you can't use the normal way of inserting strings:

    Code:
    fstream ifs;
    ifs.open ("data.str", ios::binary | ios:ut);
    
    char *data1 = "data1";
    char *data2 = "data2";
    
    ifs << data1 << endl;
    ifs << data2 << endl;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    ok that works fine, i know how to read from the file but i dont know how to read from select lines?
    like if i want to access data2 at anytime with doing ifs >> data1 first

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Anddos View Post
    ok that works fine, i know how to read from the file but i dont know how to read from select lines?
    like if i want to access data2 at anytime with doing ifs >> data1 first
    Unless each line is exactly the same size, there is no way to find the beginning of line 2, without reading line 1 first. Any alternative you can come up with will be no less expensive than that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there a reason you want to write to a binary file, though? Why not use a human readable file?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  2. reading and writing data to a binary file
    By Eulogy in forum C Programming
    Replies: 14
    Last Post: 11-24-2012, 08:38 AM
  3. Replies: 1
    Last Post: 02-29-2012, 09:52 AM
  4. Writing binary data to a file
    By zacs7 in forum C Programming
    Replies: 5
    Last Post: 10-24-2007, 04:00 PM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM