I have a text file that contains both integers and strings. There's a pattern to it, which goes:
1 2 3 4 5 6 7
Int
Int
String
Int
Int
String
...
What I want to do is obtain the first two Ints as Ints, then the string as a string. Using ifstream I can grab the first two Ints and string, but it won't move onto the next set.
I can't post the code as of now due to technical problems (I'm on a mobile) but I will if it would help. Just a small example on how you'd do this would help greatly.
getline can't place something in an int. Even using >> doesn't help however.
@Pulse
I just implanted your method and still didn't work. The first two numbers are still the only numbers available it seems and I get an error when using filename.getline(). When I changed it to getline(file, string) I get a problem I previously had which was no string being grabbed from the file.
Sorry for the lack of code. I appreciate the help.
The first two numbers are still the only numbers available it seems
Yes, u're looking for several numbers/strings so u need to store them into arrays/vectors...
1 2 3 4 5 6 7 8 9 10 11
//converting string to integer
stringstream sstream;
string str = "1234";
int integer;
sstream << str; //load the sstream with whatever is in str
sstream >> integer; //will only succeed if sstream has no characters
sstream.clear();
sstream.str(string());
//...sstream is ready to convert a new integer
i forgot that getline only works with strings... oops !