Stumped w/C++ File I/O
I''m working on a text-based RPG and i''m stumped. I have the basic classes up and running fine(player,NPC,and room). And since i don''t really want to have to hard code every single room/npc into the game i want to be able to read from an external file(.dat mebbe?). Also, this would allow me to set up a simple program where i/a player/editer can edit the files in a user-friendly environment. But i''ve run multiple times thru the VC++ 6.0 help files and BC 3.1 as well, i can''t find ANYTHING useful on how to do C++ file i/o!!
Please help!
-Run_The_Shadows
-Run_The_Shadows@excite.com
look up fstream, ofstream, ifstream.
To open a file for output create a variable of type ofstream like this
ofstream outfile( "output.txt" );
To open a file for input
ifstream infile( "input.txt" );
Then to read out of the outfile
outfile >> x;
And to read in
infile << "hello";
Simple huh? That should get you started.
Ut
To open a file for output create a variable of type ofstream like this
ofstream outfile( "output.txt" );
To open a file for input
ifstream infile( "input.txt" );
Then to read out of the outfile
outfile >> x;
And to read in
infile << "hello";
Simple huh? That should get you started.
Ut
May 12, 2000 09:59 PM
Also, I found using the "sscanf" function extremely
useful at extracting text and numbers from a line.
Example:
========
Line read from file = " TextureName=''name.bmp'' 640x480 32.0 4.5"
could be extracted like this:
"sscanf(Line, " TextureName = ''%[^'']'' %d x %d %lf %lf ", FileName, HSize, VSize, fNumber1, fNumber2);"
useful at extracting text and numbers from a line.
Example:
========
Line read from file = " TextureName=''name.bmp'' 640x480 32.0 4.5"
could be extracted like this:
"sscanf(Line, " TextureName = ''%[^'']'' %d x %d %lf %lf ", FileName, HSize, VSize, fNumber1, fNumber2);"
Thnx you two! But it still isn''t enuf i''m afraid, i can make/open/close files, but i need to be able to...
1.)Search for a particular identification number for the
room/NPC.
2.)Read the text on that particular line until it ends to
extract the info on that room/npc.
3.)Edit a particular line anywhere on it.
Like i said, i''m a slump as before i can really start the battle/movement system, i need to be able to do this crap.
Also anyone have a good extension type for this?
-Run_The_Shadows
-Run_The_Shadows@excite.com
1.)Search for a particular identification number for the
room/NPC.
2.)Read the text on that particular line until it ends to
extract the info on that room/npc.
3.)Edit a particular line anywhere on it.
Like i said, i''m a slump as before i can really start the battle/movement system, i need to be able to do this crap.
Also anyone have a good extension type for this?
-Run_The_Shadows
-Run_The_Shadows@excite.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement