I am new at C++ and i have no idea how could I search for any content at any component or text file. Could any of you plz post some examples or links to them? Thanks in forward...
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace std;
int main( int argc, char** argv )
{
if (argc != 3)
{
cout << "usage:\n "
<< argv[ 0 ] << " TEXT FILE\n\n""Search for TEXT in FILE.\n""Reports the line and column number of each occurrence.\n\n";
return 1;
}
constchar* text2match = argv[ 1 ];
constchar* filename = argv[ 2 ];
ifstream f( filename );
if (!f)
{
cerr << "Could not open file " << filename << endl;
return 1;
}
// Read the entire file into memory
string s;
string t;
while (getline( f, t ))
s += t + '\n';
f.close();
// For each match, print line number and column number
for (string::size_type index = s.find( text2match, 0 );
index != string::npos;
index = s.find( text2match, index + 1 ))
{
string::size_type line = count( s.begin(), s.begin() + index, '\n' ) + 1;
string::size_type column = index - s.rfind( '\n', index );
cout << setw( 10 ) << right
<< line
<< " : "
<< left
<< column
<< endl;
}
return 0;
}