简体   繁体   中英

c++ how to read and write within a file

i want to make a text based rpg game, but ive run into a problem, im fairly "new" to c++, know a few concepts but still learning. So what i want to know is; how do i open a file and edit the values in it? say i have a file called health, for my characters health and its set to 100. I know you can just subtract the variable by the damage you set the monsters to do, but i dont know how i store the health and edit it, while my program goes along.

ie. this is the farthest i got:

ifstream objectFile("health.txt");
string health;
double NoH;

cout << "welcome" << endl;
objectFile >> health >> NoH;
cout << health << ' ' << NoH - 15;

NoH = Number of health. So far all my program does, is read the file, and subtract 15 from 100 because thats the damage the monster do. But how do i make it "take out" the value and subtract 15 from it? then store it back so it's 85, and next time i get hit its 65 for example etc, so it makes it the objective of the game is about surviving, ie avoiding getting hit and stuff. i mean avoiding your hp to reach 0, but also i want to learn how to implement simple text based creatures you can kill, by letting their hp reach 0. thanks

what about using ini /properties file? or using json?

if you are making rpg, player save the values (hp,mp,item, map(x,y) etc) to file when saving the game? and the rest of the time should store/calculate those value in memory I guess.

about json https://code.google.com/p/vjson/

about ini file http://www.hyperrealm.com/libconfig/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM