Thread: Read and write binary file?

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007

    Read and write binary file?

    Hi all, I am trying to write a struct to a file then read it again, but it cant get it to work. It compiles file, but when I try to read it, I donít get anything. I donít think that I am writing the data to the file correctly. Because when I have a look at the file I am writing to with a hex editor I get this.
    f4 24 3d 00 14 25 3d 00 00                 Ű$=
    Below is the code, anyideas on what I am doing wrong?
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    using namespace std;
    struct ITEM {
           string txta;
           string txtb;
           } item;
    int main(int argc, char *argv[])
        item.txta = "txta";
        item.txtb = "txtb";
        cout << item.txta << " = " << item.txtb << endl;
        ofstream fout("file.dat", ios::binary);
        fout.write((char *)(&item), sizeof(item));
        item.txta = "";
        item.txtb = "";
        ifstream fin ("file.dat");
        if (fin.is_open())
            while (! fin.eof() )
                  cout << item.txta << " = " << item.txtb << endl;
        } else cout << "Unable to open file";
        return EXIT_SUCCESS;

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    RING 0
    You never include <string>. Works fine for me once I do that.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Storing strings in a file as a binary object is not going to work in the long term. You need to use some other method to store the strings than to just copy the object itself.

    A string class may look a bit like this (much simplified):
    class string
        char *buffer;
        size_t len;
        string(const char *str) 
           len = strlen(str); 
           buffer = new char[len]; 
           memcpy(buffer, str, len);
    Now, if you save that class to disk, you are going to save 8 bytes [in a 32-bit OS]: The buffer pointer and the length. The pointer is 100% sure to not point to the same string when you load it back in again [at least if you exit the application and start it again - and the ability to get the data back after exiting the application is USUALLY why we save data to a file].

    You need to store the string itself, which is what buffer in this case is pointing at.

    As to the best way of doing that in a binary file... That is a $64000 question, really - it would depend on what type of text you are storing, how much variation you expect in the sizes, how many items you expect to have, whether space or ease of programming has higher priority, etc, etc.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM