| MottMan (329) | |||
I have a text file that contains both integers and strings. There's a pattern to it, which goes:
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. Thanks in advance. | |||
|
|
|||
| soranz (472) | |
| Shouldn't be hard...maybe u could post that code so we can see... | |
|
|
|
| MottMan (329) | |
| I'll post code tomorrow at work but I can't right now due to my lack of Internet on anything but my iPad at home. | |
|
|
|
| soranz (472) | |||
err...something like this ?
| |||
|
|
|||
| Volatile Pulse (1329) | |||
Why make it more difficult than it has to be? Three separate values, three separate variables:
You have the option of doing an array/vector like sorenz posted, but this should give you the general idea. | |||
|
|
|||
| MottMan (329) | |
@soranzgetline 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. | |
|
|
|
| iantac (39) | |
|
you can use stringstream to convert string to int and vice versa http://www.cplusplus.com/reference/iostream/stringstream/ | |
|
Last edited on
|
|
| MottMan (329) | |
|
That was my first thing I did but I couldn't figure out how to change another character into another int during a loop. I may give it another try however. | |
|
|
|
| soranz (472) | ||||
Yes, u're looking for several numbers/strings so u need to store them into arrays/vectors...
i forgot that getline only works with strings... oops ! | ||||
|
Last edited on
|
||||
| MottMan (329) | |
|
I could kiss you right now. It works! What the heck does .clear() and .str() do that was so key to the success of the conversion?Thanks a ton! | |
|
|
|
| soranz (472) | |
|
Yeh, u have to keep emptying the sstream each time u want to do another conversion. clear() does the same as it does in the string class and that is it sets the content to the empty string of size 0. str(string()) as I wrote it effectively does the same thing since it sets the sstream to string() which is the empty string so u have a choice - i prefer the clear() :) Save the kiss for ur special friend MottMan :p | |
|
|
|
| MottMan (329) | |
|
Okay, thanks for the explanations. Aha, I will. It's just nice to get passed such a trivial problem. | |
|
|
|