Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


reading/writing data

This topic is 6941 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

If this is what you're getting when you look at the file, could it be something to do with the fact that you're using "wb" in the fopen function. This stands for write bytes, "wt" stands for write text.

However...I just re-read your code and you're not actually writing out the strings, you're writing out the structure, which contains char*'s, which _point_ to your text, but using fwrite won't write it to file. It'll probably just write the pointer addresses, which isn't what you want at all.
Try using fprintf instead or convert the char* to char[256].

[This message has been edited by MikeD (edited November 11, 1999).]

Share this post

Link to post
Share on other sites
I would certainly say this is due to binary storage. When you tell it to fwrite the structure, it is simply writing the memory allocated to the structure to disk. The 7 in the version field is represented as ASCII 7 (a control character) instead of '7', which is ASCII 55. And the pointers are being stored, rather than the actual character data. Since your structure contains pointers, you cannot simply fwrite it to disk. Instead, first write the strings on seperate lines like the following:

fwrite(MapInfo.MapName, strlen(MapInfo.MapName), 1, fh);

Then, write from the Version element on down, like the following:

fwrite(&(MapInfo.Version), sizeof(int)*4, 1, fh);

- Splat

Share this post

Link to post
Share on other sites
i have been working on a tile based game and i have been having troubles with writing/reading data. this is what i got:

struct MAPINFO{
char *MapName;
char *MapCreator;
char *MapDescription;
int Version;
int Background;
int Width;
int Height;


and this is what/how i want to write to the file:

MapInfo.MapName = "name";
MapInfo.MapCreator = "creator";
MapInfo.MapDescription = "short description";
MapInfo.Version = 7;
MapInfo.Background = 1;
MapInfo.Width = 255;
MapInfo.Height = 255;
FILE *fh;
fh = fopen("write.txt", "wb");
fwrite(&MapInfo, sizeof(MapInfo), 1, fh);

this seems to all work fine...it writes the version, background, width and height as characters in the text file...(ex. instead of it writing "7" (version) it writes ""...which isn't a problem at all...except it doesn't write the mapname, mapcreator, or mapdescription correctly or at all for that matter...this is what i get from it:

"`SA hSA pSA d d "

im kinda lost....if anyone can tell me wuts going on...it would help alot...thanx


Share this post

Link to post
Share on other sites

  • 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!