I am creating a program that will search a text file for a particular phrase. Immediately after that phrase will be a number that needs to be processed and entered into a new text file. On the same line of the text file will be another number that will also be processed and entered into the new text file.
I understand how to do the processing and writing to a new file. It's the searching part that I have trouble with.
I saw this code in the "getline" tutorial page.
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
I am thinking that I'll use this structure to get a line from the file, search the line and do my thing. and then continue on with the search. For my program, I do not want to extract the phrase, just the numbers to the left of the phrase. Any help?
That has an off by one error because you are looping on EOF. Please don't do that.
1 2 3 4 5 6 7 8 9 10 11 12
while (getline( myfile, line )) // while there are lines to read
{
string::size_type pos = string.find( ThePhraseYouAreLookingFor );
if (pos != string::npos) // if we found the phrase
{
// read the numbers here (see comments below for why I use seekg())
myfile.seekg( pos + ThePhraseYouAreLookingFor.length() );
myfile >> num1 >> num2;
break; // we're done, so stop searching (quit the while loop)
}
}
Your explanation was unclear whether the numbers are both on the same line as the phrase or whether they may be on another line. Hence, I used the seek function to position the file pointer and read the numbers directly. The example wasabi gave you assumes that the numbers are both on the same line as the phrase you are looking for. If the numbers are on the line following the phrase you are looking for, then you can get rid of the call to seekg(). The code I gave you will work properly either way.
The numbers I am searching for are on the same line as the key phrase, to the (correction to above) *right* of that phrase. The phrase is repeated several times in the file and I want to process each occurance.
Forgive my ignorance, I'm relearning after taking a 2 credit hour class 3 years ago that only focused on C++ for half of that semester.
Duoas:
Line 1, in preparation for the while block, reads myfile and puts the first line into a variable named line. The contents of "line" are irrelevant so long as they exist, the while loop continues.
Line 3, searches the string for the key phrase and sets variable "pos" to the location of the key phrase.
Line 4, if pos does not equal string::npos... string::npos would have a value of the last position in the string. If pos != that, pos must be somwhere in the middle of the file.
Line 7, sets a "book mark" right after the key phrase
Line 8, sends output from myfile to variables num1 and num2. I don't quite understand exactly what that output might be though.
Another question, wouldn't your code quit after the first occurance?
Also as I read your code, the "processing" I need to do would be inserted at line 9? And then remove "break" because I want to continue the search/process to the end of the file.
As a help, I'm searching a .gpx file, which is a text file for Garmin gps units, for coordinates.
The lines that I am looking for look like this:
<wpt lat="26.956467" lon="-80.14995">
I want to take 26.956467 and -80.14995, do some math and start another file with the results of the math.
After finding the first instance, I want to continue on searching the file for more instances of <wpt lat="
The numbers have a set decimal precision so I don't have to worry about them shifting around on me, I can just count characters to get to the lon.