Why isn't the whitespace removed?

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
 
J

John Harrison

William Payne said:
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

skipws is for input not output. If you want to remove the leading whitespace
you'll have to do it manually.

john
 
W

William Payne

John Harrison said:
William Payne said:
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

skipws is for input not output. If you want to remove the leading whitespace
you'll have to do it manually.

john

Oh, okay! Thanks John. I guess a combination os find_first_not_of() and
isspace() should do the trick, yes?

/ WP
 
J

John Harrison

Oh, okay! Thanks John. I guess a combination os find_first_not_of() and
isspace() should do the trick, yes?

/ WP

Here's one way

void trim_leading_whitespace(std::string& str)
{
str.erase(0, str.find_first_not_of(" \t\r\n\f"));
}

Untested code.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top