Reading txt files problem

Hey guys,

I was wondering how i could maybe right some custom code in a txt file, like:
"left 100 jump left 10 shoot"

Or some basic custom code or instructions, and make it so my program would move my character accordingly.
I am using SDL, and have a 2d character to move, but it would be cool if i could scrpit its action beforehand in a txt file, press go and it would read it and move my guy.

Also i could make it so the script would specify the distance, and my player's move functions moved on according to the distance in the txt file, and the distance value could be identified because it would be the very next value after the "left" or "right" keyword.
i was thinking like:




1
2
3
4
5
6
7
8
9
10
char x;
char y;
while (in >>x>>y)
{
if (x=="left")
 player.move(left,y);
}


{

but i dont know.. thanks again.
1
2
3
4
5
6
7
enum commands
{
    shoot = 1,
    left,
    right,
    jump
};

in file 2 100 4 4 3 10 1 //meaning left 100, jump 4, right 10, shoot

Read from the file fist number is command, have switch on it second number (or whatever you need/have in file) is the argument for the command
I have to say, I would use words in the text file. That way the computer does more work, and the person less. Then again, my memory is prob. slowly going...

But I would convert the command string to an enum to simplify the processing (I like switch statements!)

The problems with this code are:

1
2
3
4
5
6
7
    char x; // can only store a single char, use string instead
    char y; // ditto
    while (in >>x>>y) // assumes there is always a param
    {
        if (x=="left")
            player.move(left,y);
    }


I would consider
- modifying the while loop so you just read the command to start with
- you then map it to an enum value using a helper function
- and then find out if the command needs a param
- if it does, read that as a string, check it's valid and then convert it (unless your comfortable with istream error handling?)
- finally use the command id and param value, if needed

This approach should make it easier to trap and handle errors.
Last edited on
Ohh great, thanks both for fast replies, people havent been replying recently.

I was just wondering if you guys could maybe explain or show basic example code to help me understand what i should do. Your replies were great, but i just dont really understand exactly how to dot them.

Thanks again :)
So what you're asking for would be pretty easy to code once you've gotten a good feel for ifstream and ofstream. (reading from and writing to a file)

The thing you will have to do is work out your own syntax (or how your program interacts with the data that it reads from the file.)

I suggest you spend a week or two just working out all the different ways of using iostream and then come back to your main program again.
Ok, thanks alot man!

Just wondering if you guys could help me with the class structuring.
I am using SDL, and i have a main loop.

Can you guys please help me out, and tell me what classes i should have and what functions for each class? just briefly.

So like: a player class, and input class, a rendering class. And how can i read from the txt file, and apply that to the player.move class?

Thanks alot guys, i think i just struggle with OOP...
Topic archived. No new replies allowed.