I/O ps.ini

Started by
5 comments, last by Aardvajk 17 years, 12 months ago
Hey! Now that i've been doing my calculator, and worked after a while. i wanted to try to make a stats.ini (ps.ini) to load character stats, so now i'm trying to find out how to open the file, get the x (variable, can be 1 or 245 etc.) and then add 1 everytime the program is running!)

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
  int str;
  ifstream b_file ( "ps.ini" );
   b_file>> str;
   for ( str; str++; ); 
  cout<<"Process Stats Updated!";
  ofstream a_file ( "ps.ini" );
  a_file<< str;
  a_file.close();
  
   
   
     cin.get();    
     }

this is my tryout, but it just freezes everytime. STR is the variable I want to get. lets take an example. Character starts with 1 strength, so I want it to say this in the ps.ini Strength=1 . so when he gets a lvl up, I want to add 1 to strength. so STR + 1 = 2 (from the example) right? can anybody help me out?
I wanna become a good programmer for my game :-)
Advertisement
It looks like your 'for' statement is, for lack of a better expression, completely fucked.

Anatomy of a for-statement:

for ( initializer-statement; terminating-expression; iterative-statement)  statement-or-statement-block


Your code:
for ( str; str++; );


Matching up the pieces of the anatomy:
initializer-statement: "str" - it's a valid statement but doesn't do anything.
terminating-expression: "str++" - unless 'str' starts out as -1, this is always going to be non-zero (so the loop will never terminate).
iterative-statement: nothing. Officially, the loop isn't changing anything each cycle.
statement-or-statement-block: empty statement (";"). So, the body of the loop does nothing.

So, it's not surprising that it freezes - the terminating-expression is always true so it never terminates.

The way to fix it depends on what exactly you're trying to do, which I'm afraid I can't decypher from your explanation.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Xsis - I *THINK* you are trying to increment the variable to count the number of times the calculator has been run. If so, str++; or ++str; will do fine on its own, without the for().

Oh, and you should probably close b_file before you open a_file as well, since they are the same file.

b_file >> str;
++str;
b_file.close();
ofstream a_file... etc
Well, It's not for my calculator! it's for my game :-D


I know the Loop is fucked up, so here's another trying on explanation.

I wanna make a Character sheet.
So when my character lvl's up, his stats is going to increase (like in RPG)
so it has to read out the stats (in this currence, the strength)
in the ps.ini file the first one is strength=1.
then my character is going to lvl up, and his stats increases. so his strength is now 2 instead of 1.

The problem is, how can I read out the strength from the file, into my program, and add a permantly number to the number in the file? :-D
like 1+1 = 2.. then 2+1 = 3 :-)
get it now? :-)
I wanna become a good programmer for my game :-)
I'll reitterate, EasilyConfused said exactly what you want:
int str;ifstream b_file ( "ps.ini" );b_file >> str;    //get oldb_file.close();str++;  //incrementofstream a_file ( "ps.ini" );a_file << str;    //store old+1 as newa_file.close();

Thanks for the help!!!
oh sorry easyliconfused! i oversaw your post! :-(

sorry!


okay, I got another problem now. what if i want to find around in the files? so it says in the ps.ini file.


strength=1
agility=1
wisdom=1
health=76
cosmo=20


how can I program it so it regonizes only the numbers but still changes the correct ´parts, so it says "Level Up!"

strength=2
agility=2
wisdom=2
health=88
cosmo=25

just an example! but how?
I wanna become a good programmer for my game :-)
Xsis, this is a lot more complicated and far to general a question to get a helpful answer.

You would probably be advised to:

a) investigate std::string and reading lines of text from a std::istream with getline()

b) have a look into basic text parsing or std::string's member functions for splitting up strings

c) have a look at stringstreams or std::atoi for converting a string number to an integer

and post back when you have more specific questions.

HTH

Paul

This topic is closed to new replies.

Advertisement