If I were making a game, could I use fstreams to change stuff in real time?

May 11, 2013 at 5:05pm
If I had a text file that said "Player speed = 5"

Could I use an fstream to be constantly reading that so that I could go into the text file and change it to "Player speed = 10" and it would instantly happen in the program

Is there a better way to do something like that?
May 11, 2013 at 5:35pm
It won't necessarily happen "instantly" but you could technically use a loop to do something like that here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
int main ( void )
{
    unsigned int i = 0;
    do
    {
        std::ifstream in( "example.txt" );
        in >> i;
        in.close();
        std::cout << i << std::endl;
        i++;
        std::ofstream out( "example.txt" );
        out << i;
        out.close();
    } while( i < 15 );
}
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Process returned 0 (0x0)   execution time : 0.647 s
Press any key to continue.


The text file started at 0 and ended at 15; (because it reads increments then writes and it doesn't increment write then read)
May 11, 2013 at 5:44pm
Why not use a class or struct to handle all the changes to the player's attributes?
Topic archived. No new replies allowed.