Advertisement

File I/O

Started by June 05, 2002 08:57 AM
4 comments, last by hammerstein_02 22 years, 3 months ago
I am wanting to create level files for my game, and don''t quite know how to go about it. My file format so far is, char Name[25]; int Size_X; int Size_Y; int numenemies; char graphics[255]; EnemyDetails1; EnemyDetails2; ... I am creating a shootem-up game, I know that I will need these details for each level I create, and my program will read a main file which will contain the filenames of my levels. getting the filenames from this main file I can deal with. Enemydetails will be data for each enemy. Can I read/write structs to file? Or do I just have to create a function that will read each member of a struct in? I don''t quite understand how I go about this..
You should put all these variables in a Struct and simply:
fread(FILE *, Struct *, sizeof(Struct))
and :
fwrite(FILE *, Struct *, sizeof(Struct))
from and to file opened with fopen().
You can find info on those in your c books and help files.
Advertisement
Yes you can read and write structures to a file heres how to do it in C++ streams, first you save your structure to file, from your level editor say, (I''ll assume the structure you make is called STRUCT for the example).


  #include <fstream.h>STRUCT OutputStructure;// FILL STRUCTURE ECT.ofstream OutputStream("Datafile.dat",ios::binary);OutputStream.write((char*)&OutputStructure,sizeof(STRUCT));OutputStream.close();  


Then in your game to read it back


  #include <fstream.h>STRUCT InputStructure;ifstream InputStream("Datafile.dat",ios::binary);InputStream.read((char*)&InputStructure,sizeof(STRUCT));InputStream.close();  


Ofcourse you can write more than one structure or class to a file. One thing to remember, if you have pointers in your structure that point to structures or variables that you declare on the freestore, then it won''t work, then you have to make a function to save the structure and usually save a header to the file saying how large everything is.

Ballistic Programs
Cheers for the replies guys, that helped. Drazgal, how come you use the address of when reading/writing to the file? Should I do it that way? I did it without and seem to be able to read/write fine..

I know this might add a level of complication, but if I was to create a level editor in Delphi (I am more used to visual development in delphi) and exported the files, would there be any problem reading them back in my cpp program?
There shouldnt be any problem if you do it right.
If you save all variables in the same order, there shouldn''t be any problem. Of course if your Types in Delphi are different than in C you''ll need to do extra work to addapt.
I do it because thats what I was told to do when learning streams That and I''ve acidently left them out before and Ive had a few bugs that were solvd by placing the address instead. can''t remember for the life of me though what the bugs were.

Ballistic Programs

This topic is closed to new replies.

Advertisement