Advertisement

File Handling in C++

Started by July 31, 2000 02:09 PM
3 comments, last by gavco98 24 years, 5 months ago
Hi guys, im writing a basic game using Borland C++ Builder, and need to save some data to disk. I have tried using loads of different functions (fopen etc.), but when i write the contents of a variable to file it appears corrupt. Anyone know of a tutorial that demonstates a method that works in win32? G Coates ------------------------------------------- Software is like sex: better when its free!
Gavin Coates
[size="1"]IT Engineer / Web Developer / Aviation Consultant
[size="1"][ Taxiway Alpha ] [ Personal Home Page ]
Sorry, but what do you mean with "Appears corrupt"?
when you write a numeric variable, what is put to the file is the binary value of that variable; So if you have an int and you use fwrite(&anInt,4,1,myfile) it''ll write binary values wich won''t be readable by a text editor, like notepad.

You know, I never wanted to be a programmer...

Alexandre Moura
Advertisement
Just pointing something out: fwrite(&anInt,4,1,myfile) isn''t very good. You should use fwrite(&anInt,sizeof(int),1,myfile) instead. Much more portable.

Gavco, post some of the file handling code you use.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
It is possible that you are trying to write a binary file, in which case you will probably not be able to read (yourself, that is) things stored in it - try opening the file as a text stream, then you should be able to read stuff stored in it, but you shouldn''t use fwrite() and fread() with text streams I believe.

Note when I say read, I mean open the file in a text editor and look at it.

-Mezz
Try using file streaming. There is a very good tutorial on this site that explains it. Not sure of the exact address.. you''ll have to look for it in the programming section.

- Daniel
VG Games
- DanielMy homepage

This topic is closed to new replies.

Advertisement