Sign in to follow this  
TEUTON

writing structures to file using fwrite

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
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
Quote:
Original post by Anonymous Poster
wtfm8? Are you retarded?


I think it's you who's retarded. You were the one answering something which I haven't even asked.

Go away you scared brat or next time post with your username.

Share this post


Link to post
Share on other sites
Quote:
Original post by TEUTON
Quote:
Original post by Anonymous Poster
wtfm8? Are you retarded?


I think it's you who's retarded. You were the one answering something which I haven't even asked.

Go away you scared brat or next time post with your username.


Formatted my computer and my email was down for a while, its back up now.
Scared brat? Sorry for not flaming you more. This was the beginner forum last time I checked.
You need to chill bud. Especially when you are asking for help (and receiving it). Where do you get off giving lip to someone trying to help you? How old could you possibly be? I'm going to guess 14, how close am I?

Share this post


Link to post
Share on other sites
Actually I really hate this concept of anonymous poster.

And I am totally chilled. That anonymous poster was no way helping me. Yes, I do

got irritated at that moment because that reply came from no where and it didn't

helped at all. And if you think rating me bad is actually gonna affect me then you

are wrong. I don't really mind all people here start posting me bad. Rating

actually doesn't mean anything.

I know this talk will go on and on....so moderator plz close this thread.

Thank You Everyone



Share this post


Link to post
Share on other sites
Quote:
Original post by TEUTON
Actually I really hate this concept of anonymous poster.

That anonymous poster...


... is/was me if you were listening. And I did help you. This is chess, not checkers. If you don't want to carry out the trash in your code, try java. If you would have explained your question better (told me what the problem you were having was), I would have given you a better answer. The way you originally had it would have worked, it was just bad code.

Share this post


Link to post
Share on other sites
This is a beginner forum and blaze02 was trying helping you to remember to use fclose() after you are finished using the file. He doesn't know how much you know of C, you might know very well to close the files after your finished using them but didn't feel the need to include it in your code. Once again, blaze02 doesn't know this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this