Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

writing structures to file using fwrite

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am able to enter structure containing one type of element in the file using this code.
#include<stdio.h>
struct a
	{
		char x[3];
		//int n;
	}obj={"ABC"/*,4*/};
int main()
{
FILE *fp;

fp=fopen("Data", "w");
fwrite(&obj, sizeof(char), 3, fp);
return 0;
}
But I am not able to figure out the way to write different type of data contained in the structure all together in the file.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Make sure you close the file with fclose(...)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Make sure you close the file with fclose(...)


No I won't....do whatever you want to...

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
fwrite(&obj, sizeof(obj), 1, fp);


It was that simple. Wow.
But now file contains some strange characters.

Share this post


Link to post
Share on other sites
The fwrite() command is used for binary writing, so whatever you write is not comprehensible through a text viewer, unless it contains character strings (this is why people can look in executables and find words).

You should open the file in binary mode like this:

fp=fopen("Data", "wb");

Share this post


Link to post
Share on other sites
But I have character strings in my structure. File still contains some strange characters. n yes, I have changed the mode to "wb".

Share this post


Link to post
Share on other sites
This is serialization. You can look into Boost.

Or you can simply implement a save function which goes through each member variable and saves it appropriately to the text file (watch out for pointers) and then you can have a load function which reads each member variable back in order. In this case you do actually want text mode I/O.

Share this post


Link to post
Share on other sites
I don't know how much i got what you told me. But can you tell me now how can I read the binary file and display it's data on the console.

I am not sure what to put in fread(...)function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by TEUTON
Quote:
Original post by Anonymous Poster
Make sure you close the file with fclose(...)


No I won't....do whatever you want to...


wtfm8? Are you retarded?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!