Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search file 1

Status
Not open for further replies.

mactonio

Programmer
Nov 18, 2003
55
0
0
US
hi guys,
what is the easiest and most efficient way to search a text file for a keyword.

eg. I have a text file with contents like so

mytext.txt:

stuff stuff {
more stuff ....
}
and again more stuff
...
...
DATASOURCE = ot2 ... ....
even more stuff ...
..


and so on. What i want to do is read this file search for the line with "DATASOURCE =" and grab 'ot2', this line could be anywhere in the file.

thank for any help.
Chris
 
Reading one line at a time and searching that line would probably be the best way. If it's a small file, you can just read the whole thing into a string and search that.
 
ok....so if i read in line by line, when i get to the line i want, eg

'DATASOURCE = ot2 7001'

how do i check that the line contains DATASOURCE and if it does save the first variable that comes after the = sign, for example in the above i would want to store ot2....i know how to do this in perl with regex but just started to use c++

thanks,
Chris
 
Assuming you're using an STL string to store each line, use the find() member function to search for your search string.
When you find a line with your search string, use find() again to search for '='. find() will return the position of the '=' in the string. Use the substr() member function to return everything after that position + 1, then search for and remove any whitespace in there...
 
can someone please give me a brief example on how to do the above with some code. This is a side task for me and I am really a novice in c++ and just havent had the chance to research what STL string is and how to use the find() and substr() functions as what cpjust suggested above.

I know this is something simple, because its like two lines in perl.
 
Something like this should work:
Code:
ifstream inFile( "myfile.txt" );

if ( !inFile )
{
   cout << endl << "Error opening the file!" << endl;
   // Return an error here.
}

string str;
string searchStr( "DATASOURCE =" );
string subStr;
size_t pos = 0;

while ( getline( inFile, str ) )
{
   pos = str.find( searchStr );

   if ( pos != string::npos )
   {
      // Found the string.
      subStr = str.substr( pos + searchStr.size() );
      break;
   }
}
subStr will be set to anything after the "DATASOURCE =" until the end of the line, or to an empty string if "DATASOURCE =" was not found.
 
F.Y.I. - i found a much easier way using split function.

this is what i used in my .NET application;

using namespace System::Text::RegularExpressions;
...

String* input = _T("DATASOURCE = ot4 7001 pfr");
Regex* regex = new Regex(S" "); // will be splitting on white spaces
String* tokens[] = regex->Split(input);


and thats it, now tokens[] has all the items separated by white space, so if i want ot2 all i have to do is get token[2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top