Reading Through a File of Ints and Strings

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.

Thanks in advance.
Shouldn't be hard...maybe u could post that code so we can see...
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.
err...something like this ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ifstream fin;
fin.open("hello.txt");

string sFile[MAX] = "";
int iFile[MAX] = 0;

int i, j = 0;
while(fin)
{

   getline(fin, iFile[i]);
   getline(fin, iFile[i+1]);
   getline(fin, sFile[j]);

   i += 2;
   j+= 1;
}
Why make it more difficult than it has to be? Three separate values, three separate variables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>

int main() {
   int myInt1, myInt2;
   std::string myString;

   std::ifstream inFile("file.txt");

   while(inFile.good()) {
      inFile >> myInt1 >> myInt2;
      inFile.getline(myString);

      std::cout << myInt1 << "\n" << myInt2 << "\n" << myString;
   }
}


You have the option of doing an array/vector like sorenz posted, but this should give you the general idea.
@soranz

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.
you can use stringstream to convert string to int and vice versa
http://www.cplusplus.com/reference/iostream/stringstream/
Last edited on
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.
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 !
Last edited on
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!
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
Okay, thanks for the explanations.

Aha, I will. It's just nice to get passed such a trivial problem.
Topic archived. No new replies allowed.