Jump to content
  • Advertisement

Archived

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

Arkantis

file io

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

Does anyone have a great link to working with files?.... I understand how to use the fstream, but I dont fully understand how files are written and such... Like I can write a structure of data to a file but say I want to write the same structure between two other structures in a file.... how do I do that? is it easier to do such things with binary files?... Also why/how binary files? arg, me==lost;.....

Share this post


Link to post
Share on other sites
Advertisement
Most of the time you 'll use binary files with data. not that ascii files are not usefull with data, they are. eg. *.ini files. You'll use them when you want to be able to read and change the file yourself, not just your program. If you don't care about that then binary files are the easiest to navigate/read/write data cause they are just a pure dump of data - no formating and the like.

Lets say you've stored 10 structures of type MyType to a binary file. To read the 5th structure, you can move the read file pointer to the beginning of the file and move it 5*sizeof(MyType). if you want to insert 2 more structs at position 7 but you don't want to ovewrite structs 7 and 8, you read structs 7-10, you store them in memory or in a temp file, you ovewrite 7 and 8, and then you append the saved structures.

Edit: that was also a dump of data(random thought structs), in the spirit of binary files. :D hope this helped
___________________________________________________
VRAM_Strider.

"This is the same damn ship that blows panels everytime it is shot, gets stolen on numerous occasions, has next to no security on any of its computer systems, allows almost anyone into the heart of the ship, and places the bridge and all the exec offices on the top of the ship?That enterprise ?"

[edited by - VStrider on June 7, 2004 4:02:53 AM]

Share this post


Link to post
Share on other sites
First of all, you shouldn''t write structures (assuming you mean C/C++ structs) to files *at all*. Everyone does it right now, but portability is becoming increasingly important, and this practice might not work on different systems and/or compilers, because a compiler can add padding to a structure.

Instead, every structure you want to write should have means for serialization, i.e. turning itself into a stream of bytes.

Now for binary files: They only exist on windows. By default, windows opens files in ASCII mode. This means that the OS will automatically process the \r\n newlines into \n automatically. Also, IIRC, encountering an EOF character in ASCII mode is treated as the end of the file.

So, obviously, if you want to store binary data, you should use binary mode.

---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://zqf.de/randomz/

Share this post


Link to post
Share on other sites
quote:
Original post by randomZ
First of all, you shouldn''t write structures (assuming you mean C/C++ structs) to files *at all*. Everyone does it right now, but portability is becoming increasingly important, and this practice might not work on different systems and/or compilers, because a compiler can add padding to a structure.

Instead, every structure you want to write should have means for serialization, i.e. turning itself into a stream of bytes.

I see your point, but the only case you would have a problem porting it, would be if you need to use the same binary data file with different installations. A rare case. If this is what you need to do, then yes, serialization maybe more appropriate. or even a DBMS if you have too much data.

___________________________________________________
VRAM_Strider.

[size=1]"This is the same damn ship that blows panels everytime it is shot, gets stolen on numerous occasions, has next to no security on any of its computer systems, allows almost anyone into the heart of the ship, and places the bridge and all the exec offices on the top of the ship?That enterprise ?" [/size]

Share this post


Link to post
Share on other sites
A rare case? Consider map or model files. Also, if I couldn't exchange save games between e.g. the windows and linux versions of a game I'd be pretty disappointed.

Actually, what type of files wouldn't need to be exchangeable between different installations of a game?

edit: BTW, you can use normal HTML tags in your sig.

[edited by - randomZ on June 8, 2004 4:41:45 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by randomZ
A rare case? Consider map or model files. Also, if I couldn''t exchange save games between e.g. the windows and linux versions of a game I''d be pretty disappointed.

Obviously for these files you will not want to use plain binary dumps. Especially if your software targets more than 1 platform. But if your game targets only 1 platform, like windows, you wouldn''t have a problem. heh i forgot, this doesn''t happen often.

quote:
Original post by randomZ Actually, what type of files wouldn''t need to be exchangeable between different installations of a game?

You would probably use binary files for something quick and dirty that does not need to be used on another instalation.

btw you can tell the compiler not to pad certain structs. but then again, you might still have issues between systems, like the size of built-in types could be different.

We went OT. Arkantis question was not what files are more suited for multiplatform development but how to use binary files and if there are easier ways to do these things.

quote:
Original post by randomZ
edit: BTW, you can use normal HTML tags in your sig.

thx

VStrider.

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!