Hi all,
I am a relatively inexperienced programmer, but I am really eager to do the hard yards and learn. To produce the code below, I have spent about 4 hours reading textbooks and searching the internet, so try not to laugh at it! Thanks in advance for any advice you may offer! My problem is...
I have a text file with the following text inside it
Code:
image 0
281253 6.13325e+06 34.8823 0.009
-3.13852 0.0117609 -1.07777
1170 1189 1279 1278 ...
... (and then the next lot of information that is correlated with the next image)
image 1
234940 6.13232e+06 ...etc
So what I want to do is:
1. Search the code for the text "image"
2. Store the number after the text "image"
3. Store all of the following numbers, until the next mention of the word "image" OR until the end of the file.
So my attempt was to do this:
Code:
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int main()
{
int imageNumber = -1;
ifstream inFile;
string searchString = "image";
string lineOfText;
inFile.open("C:\\datafile.txt");
for(;;){
getline(inFile, lineOfText);
if (inFile.eof()) break;
if (lineOfText.find(searchString, 0) != string::npos){
cout << "The program found the word: " << searchString << endl;
inFile >> imageNumber;
cout << "imageNumber read: " << imageNumber << "\n";
/* etc...
I read in the other numbers using:
inFile >> variableName;
and then read in the 4 digit numbers into an array using
a for loop (which works well)
*/
}
}
getch();
return 0;
}
The output (which is not quite what I want) is:
Code:
The program found the word: image
imageNumber read: 281253
I want the imageNumber to read the number after the text "image", i.e. '0' in this case. The problem is because the lineOfText.find() has searched the whole line, and now the image stream pointer (correct me if I am wrong ) is pointing to the next line, so imageNumber just picks up the first integer on that line.
I never like coming to people for help with empty hands, so I think I either need to:
1. Use tokenisers - I read the forum help on this and vaguely understood it (it'll take me another 4 hours or so to work out how to integrate search with them though...)
2. Stop the pointer after matching the word "image" so I can read in the correct imageNumber variable. I have no idea how. Maybe seekg()? I read about that a few times.
Once again, I am only a beginner, so I would prefer you to help me with this code if possible rather than having to get my head around another concept.
Thanks so much for any suggestions (particularly code).
Regards, Geek10