CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    how save a structure object on a file and read it?

    see these code:
    Code:
    struct user
    {
        string name;
        string  adress;
        image foto;//i must overloading ofstream and ifstream operators
        int age=0;
    };
    
    user usrName[2];
    user usrName2[2];
    
    //save a structure object to a file
    template<typename structure>
    void SaveDataBase(string filename,structure &StructureVariable )
    {
        remove(filename.c_str());
        ofstream writefile;
        writefile.open(filename.c_str(), ios::out | ios::binary);
        writefile.write (reinterpret_cast<char*>(StructureVariable),sizeof(structure));
        writefile.close();
    }
    
    //read a structure object from a file
    template<typename structure>
    void ReadDataBase(string filename,structure &StructureVariable )
    {
        ifstream readfile;
        readfile.open(filename.c_str(), ios::out | ios::binary);
        readfile.read (reinterpret_cast<char*>(StructureVariable),sizeof(structure));
        readfile.close();
    }
    on my image class i'm trying overloading ofstream\ifstream operators:
    Code:
    friend ofstream& operator << (ofstream &os, const image &img2)
        {
            int stringsize=img2.strfilename.size()+1;
            char chrFileName[img2.strfilename.size()+1];
            sprintf(chrFileName,"%s",img2.strfilename.c_str());
            os << stringsize << chrFileName;
            return os;
        }
    
        friend ifstream& operator >> (ifstream &is, image &img2)
        {
            DebugText("reading file");
            int stringsize;
            is >>stringsize;
            char chFileName[stringsize];
            is >>chFileName;
            img2.readimagefile(chFileName);
            DebugText("end of reading file");
            return is;
        }
    but why these overloading operators aren't called?
    i'm doing something wrong, but i'm confused
    my objective is overloading the ofstream.write() and ifstream.read().
    i can save the usrName on a file and read it to usrName2. but when i close the application, the Windows give me an error: 'the program stop responding' and then close it. i'm trying fix these error

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how save a structure object on a file and read it?

    You are not calling the overload because for the simple reasons that:
    1. You are casting your object's address to char*, doing a raw read/wrtie, meaning you never actually call anything on it.
    2. You are never calling << or >> anyways.

    Note that what you are doing will virtually never work, as you are writing the literal memory contents of your object. The issue is that:
    1. It's layout is depends on the platform and release the binary was compiled with. Even on a same machine!
    2. It completely bypasses constructors on reading, potentially filling your objects with junk.
    3. It has no knowledge of memory layout: A string is usually a pointer to a char buffer, but all you will be saving is a pointer (utterlly useless).

    I suggest you read about: http://www.parashift.com/c++-faq/serialization.html

    Using an actual serizalization library could help, in particular, http://www.boost.org/doc/libs/1_59_0...doc/index.html

    If this is meant for class, or something very lightweight, then I'd suggest you simply go for something more like a CSV/TSV.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured