I'm working on a simple function to read in from a file called save.txt and i need to read about 7 or 8 characters in. 4 characters are numbers that represent North south east and west respectively the other 4 characters are delimiters(i don't know why i put them in maybe for easier reading?)
and the function is supposed to "load" the numbers up in int north,south,east,west; but im kinda having some trouble with that part. a little point in the right direction would be swell i have tried a combination of getline() strok() and something else(can't remember).

Short Version:
load text from a file and
assign the numbers to their respective integers.

anyway here is what I've have right now when i was messing with strok().

if(input=="load"){
        char locat[8]; //location is already a string so thats why this is name locat.
        char* test=NULL;
        int loop=0;
        cout<<"loading..\n";
        load.open("save.txt");
        load.getline(locat,8);
        test=strtok(locat,".");
        while(test!=NULL){
            cout<<test<<"\n";
            test=strtok(NULL,".");
        }
    }

and sample save.txt

3.0.0.1
//North first. South second. East third. and West fourth.

Edited 6 Years Ago by avarionist: n/a

4 characters are numbers that represent North south east and west respectively the other 4 characters are delimiters(i don't know why i put them in maybe for easier reading?)

No, it makes for more difficult reading. If the file format is one that you created yourself, you are better off replacing the periods in the file with blank spaces and using the >> operator. No need to use strtok. No characters, no strings. Just plain old integers read directly from the file into the variables.

int north, south, east, west;

ifstream load;
load.open("save.txt");
load >> north >> south >> east >> west;
load.close();
cout << north << '\t' << south << '\t' << east << '\t' << west << '\n';

File:

3 0 0 1
//North first. South second. East third. and West fourth (spaces, not periods)[B].[/B]

Edited 6 Years Ago by VernonDozier: n/a

Thanks i was under the impression that you would have to load the values into a char array/string and then split and convert them into a int but thanks that really helps :D
could you explain why it works ? so that i may better understand it. logically to me i can't figure it out. when it reads from the file and i do load>>north>>south etc does it read until it finds a space and then loads the variable with the number ?

Edited 6 Years Ago by avarionist: n/a

Thanks i was under the impression that you would have to load the values into a char array/string and then split and convert them into a int but thanks that really helps :D
could you explain why it works ? so that i may better understand it. logically to me i can't figure it out. when it reads from the file and i do load>>north>>south etc does it read until it finds a space and then loads the variable with the number ?

You're welcome. The >> operator:

http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

looks at the type of variable that is being supplied and will behave differently depending on what that variable is. You have supplied the >> operator an int variable, so it will:

  1. Skip all white space(tabs, carriage returns, spaces, line returns).
  2. Look for a digit or a minus sign as the first character it's interested in
  3. Keep reading a digit at a time till it sees a non-digit.
  4. Stop.
  5. Put what it read into the variable.

Exactly how the >> operator parses it behind the scenes is something I don't know, but the whole point of C++ is that you don't HAVE to know how any of the underlying code is written. It's written for you and it's a black box. You don't know or care how it's done. Just remember that white space is treated as a delimiter and you can take advantage of that.

thanks guys :D Would it be possible to add a delimiter for >> or would i have to overload it ?

This question has already been answered. Start a new discussion instead.