please...i need a help writing a struct to a binary file!!!!

This is a discussion on please...i need a help writing a struct to a binary file!!!! within the C++ Programming forums, part of the General Programming Boards category; hi guys i am wondering whether i can write a struct to a binary file or not?! well , in ...

  1. #1
    Registered User sam-j89's Avatar
    Join Date
    May 2009
    Posts
    14

    Exclamation please...i need a help writing a struct to a binary file!!!!

    hi guys
    i am wondering whether i can write a struct to a binary file or not?!
    well , in this segment of my program :
    Code:
    enum Status {full, busy, empty};
    
    struct block
    {
    	char* WrdsArray[100];
    	Status state;
    	int NumberOfWrds;
    	int NumberOfBlock;
    	
    };
    
    
    
    int main()
    {
    	fstream myfile;
    	myfile.open("file1.bin",ios::binary|ios::out);
    	block b;
    	for(int i = 0; i<100; i++)
    	{
    		b.WrdsArray[i] = " NON ";
    	}
    	b.NumberOfBlock = 0;
    	b.NumberOfWrds = 0;
    	b.state = empty;
    	myfile.write(reinterpret_cast<char*>(b),512);
    	myfile.close();
    when i wanna have my program compiled , i get the error :
    error C2440: 'reinterpret_cast' : cannot convert from 'block' to 'char *'
    and when i try to write in another format , like:
    Code:
    myfile<<b;
    instead of
    Code:
    myfile.write(reinterpret_cast<char*>(b),512);
    i get the compiler error :
    error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'block' (or there is no acceptable conversion)
    so plzz guys can anyone lead me how to write this struct to my binary file?!!!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    myfile.write(reinterpret_cast<char*>(b),512);
    To write out the structure, that should be:
    Code:
    myfile.write(reinterpret_cast<char*>(&b),sizeof(b));
    However, you are writing an object that contains pointers which is going to be meaningless once you read in the object because you are only writing/reading pointers(addresses) which don't have any useful meaning. For that you'd need to write out the actual data that those pointers point to and not just the address and this means some type of serialization of input/output.

    Quote Originally Posted by sam-j89
    and when i try to write in another format , like:
    Code:
    myfile<<b;
    That requires you to overload the << operator which I do not see you doing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by sam-j89 View Post
    Code:
    enum Status {full, busy, empty};
    
    struct block
    {
    	char* WrdsArray[100];
    	Status state;
    	int NumberOfWrds;
    	int NumberOfBlock;
    	
    };
    in this struct, you are declaring an array of pointers, which the program will have no problem writing to the file, but the data written will probably not be what you are expecting. the pointer array will be written, but not the data it points to.

    iinstead, try the following:

    Code:
    enum Status {full, busy, empty};
    
    struct block
    {
    	char WrdsArray[100][100];
    	Status state;
    	int NumberOfWrds;
    	int NumberOfBlock;
    	
    };
    that will allocate space for 100 char arrays of size 100, which will be properly written to the file. also, you have a member of type 'Status'. if this is a struct, ensure that it does not contain any pointers. replace pointer members with actual objects. when you write a struct to a file, you only copy the data between the curly braces. the data that members point to is not replicated.

    Code:
    int main()
    {
    	fstream myfile;
    	myfile.open("file1.bin",ios::binary|ios::out);
    	block b;
    	for(int i = 0; i<100; i++)
    	{
    		b.WrdsArray[i] = " NON ";
    	}
    	b.NumberOfBlock = 0;
    	b.NumberOfWrds = 0;
    	b.state = empty;
    	myfile.write(reinterpret_cast<char*>(b),512);
    	myfile.close();
    when i wanna have my program compiled , i get the error :
    error C2440: 'reinterpret_cast' : cannot convert from 'block' to 'char *'
    that's because 'b' is not a pointer. you can't convert a struct directly to a char pointer. you can, however convert a pointer to that stuct (by using the address-of operator) to a char pointer like so:

    Code:
    myfile.write(reinterpret_cast<char*>(&b),sizeof(block));
    you'll note that I also used the sizeof() operator. this way, you don't have to remember how big your struct is, and also, by specifying a size bigger than your struct, you could potentially read past the end of memory allocated to your process, and have undefined behavior, including corrupt data and frequent crashes.

    you will also have to replace

    Code:
    b.WrdsArray[i] = " NON ";
    with

    Code:
    strcpy(b.WrdsArray[i], " NON ");
    because you can't use = to assign the contents of a c-style array.

    and when i try to write in another format , like:
    Code:
    myfile<<b;
    instead of
    Code:
    myfile.write(reinterpret_cast<char*>(b),512);
    i get the compiler error :
    error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'block' (or there is no acceptable conversion)
    if you want to use the << or >> operators with your structs, you need to define those operator overloads yourself. like so:

    Code:
    std::ostream& operator<< (std::ostream& os, const block& b)
    {
      return os.write(reinterpret_cast<char*>(&b), sizeof(block));
    }
    hope this was helpful

  4. #4
    Registered User sam-j89's Avatar
    Join Date
    May 2009
    Posts
    14
    thank you very much guys for your useful advices ... my program now is working successfully

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    one more thing:

    don't try to write objects like std::string or std::vector or any other STL container to a file. these objects also contain pointers, and you are not likely to get useful results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  3. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  4. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21