Jump to content
  • Advertisement


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


Stumped w/C++ File I/O

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

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

Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Also, I found using the "sscanf" function extremely
useful at extracting text and numbers from a line.

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);"

Share this post

Link to post
Share on other sites
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
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?


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!