By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Why isn't the whitespace removed?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"William Payne" <mi**************@student.liu.se> wrote in message
news:ce********@news.island.liu.se...
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
Jul 22 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2n************@uni-berlin.de...

"William Payne" <mi**************@student.liu.se> wrote in message
news:ce********@news.island.liu.se...
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
Jul 22 '05 #3

P: n/a
> >
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


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.