W
William Payne
Hello, I'm working on my own variant of grep and I have an annoying astethic
(spelling) problem. Say the user is searching for the string "foo" in all
files of type .bar in a given directory (recursively). When a match is found
I want to output the file name including full path and line number and on
the next line the complete matching line. However, I wish to remove any
leading whitespace when outputting the matching line so I am using
std::skipws ,but it's not removed. How do I fix it?
The code is as follows (not a complete program):
void scan_for_string(const string& file_name,
const string& pattern)
{
ifstream file(file_name.c_str());
if(!file)
{
char error_message[MAX_PATH + 128];
sprintf(error_message,
"Error opening file %s",
file_name.c_str());
throw runtime_error(error_message);
}
string line;
size_t line_number = 0;
while(getline(file, line))
{
++line_number;
if(line.find(pattern) != string::npos)
{
cout << file_name << ":" << line_number << ":"
<< endl << std::skipws << line << endl << endl;
}
}
file.close();
}
Thanks for any replies
/ WP
(spelling) problem. Say the user is searching for the string "foo" in all
files of type .bar in a given directory (recursively). When a match is found
I want to output the file name including full path and line number and on
the next line the complete matching line. However, I wish to remove any
leading whitespace when outputting the matching line so I am using
std::skipws ,but it's not removed. How do I fix it?
The code is as follows (not a complete program):
void scan_for_string(const string& file_name,
const string& pattern)
{
ifstream file(file_name.c_str());
if(!file)
{
char error_message[MAX_PATH + 128];
sprintf(error_message,
"Error opening file %s",
file_name.c_str());
throw runtime_error(error_message);
}
string line;
size_t line_number = 0;
while(getline(file, line))
{
++line_number;
if(line.find(pattern) != string::npos)
{
cout << file_name << ":" << line_number << ":"
<< endl << std::skipws << line << endl << endl;
}
}
file.close();
}
Thanks for any replies
/ WP