Thread: Problem with Binary file I/O

  1. #1
    Registered User Tankists's Avatar
    Join Date
    Feb 2009
    Posts
    2

    Question Problem with Binary file I/O

    Hey, I came here to ask for help with my little project, where I need to write to binary file and read from it with other program. I made a simple test programs that does the thing I need - One of them writes a simple struct with 2 member variables (std::string and int), and the second program reads that file, using the same struct. all went well with int type variable, but std::string variable didn`t showed at all or made program crash.

    Here is bouth sources of those simple programs:

    Writer
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    struct CTest
    {
        int number;
        std::string text;
    };
    
    int main()
    {
        std::ofstream output("level.map", std::ios::binary);
    
        CTest obj;
    
        obj.number = 255;
        obj.text = "testing...";
    
        output.write((const char*)(&obj), sizeof(obj));
    
        output.close();
    
        std::cin.get();
    
        return 0;
    }
    Reader
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    struct CTest
    {
        int number;
        std::string text;
    };
    
    int main()
    {
        std::ifstream input("level.map", std::ios::binary);
    
        CTest buffer;
    
        input.read((char*)(&buffer), sizeof(buffer));
    
        std::cout << buffer.number << std::endl;
    
        std::cout << buffer.text << std::endl;
    
        std::cin.get();
    
        return 0;
    }
    What am I doing wrong, and how to write struct with std::string type member correctly? Hope to get some help.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The std::string made it crash because it internal pointers to things that aren't contained inside its data structure. since those pointers don't point to anything you've allocated in your program, it will fail. for storing data in files, it's usually a good idea to use C++ native types.

    try this:
    Code:
    struct CTest
    {
        int number;
        char text[50];
    };
    that will almost certainly work, but you'll have to do a string copy to set the value of the text member in your writer. Remember that you have to set a size on the text member. you can't just have a pointer and dynamically allocate it, otherwise it will store the pointer in the file, and not the data you wanted. much like the problem you're already having.

  3. #3
    Registered User Tankists's Avatar
    Join Date
    Feb 2009
    Posts
    2
    Thanks, this helped me a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. File I/O problem
    By dacbo in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 08:22 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM