loading data from a text file

killrj (6)
hey guys,
hope you guys could help me.
i need to load data from a text file
i was able to load data separated by commas "," using this code:

1
2
3
        stringstream (loaded.substr(0,loaded.find(","))) >> temp;
        player1_turn = temp; //assigns the data to variable player1_turn
        loaded = loaded.substr(loaded.find(",")+1,loaded.length());

but i don't understand what this code does or how to modify it to read data after a string.
for example the data in the file is such=

p1HIT:1,3p1MISS:4,4 //endl
p2MISS:1,0

i want to extract the numbers for use and discard the strings and separators.

thanks in advance for any help!.
pogrady (525)
Consider std::istream.getline(char* c, std::streamsize n, char delimitor).

http://www.cplusplus.com/reference/istream/istream/getline/

Anytime the documentation reads "extracts" it means that the character no longer is in the input stream.
killrj (6)
thanks for the reply
but what does the code i used do exactly?

and can you please give an example of how getline is used to get data and assign it to a variable?
Aceix (476)
What your code does is simply to:

-Assign temp the content of loaded. ie: from the first character in the string to the first comma(because, find() returns the position of where the first comma was found)
-the it assigns player1_turn the value of temp
-Then it also assigns loaded, what. Loaded contains from the character after the first comma, to the end of the string.

HTH,
Aceix.
killrj (6)
great explanation aceix!
thanks a lot ill try to go from there to solve my issue

though if i could get a tip on the best way to read from a file with the following content it would be immensely appreciated=

1
2
3
1,1,2,2,1,3,1,2,4,2,5,2,0,0,1,0,0,0,99,99,99,99,99,6,0,0,0,0,1,0,jerard,hanoi,0
p1HIT:1,3p1MISS:4,4
p2MISS:1,0


i was able to read and load all the content in the first line, my problem starts from the second line onwards.
Registered users can post here. Sign in or register to post.