473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why isn't the whitespace removed?

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
3 1933

"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

"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
> >
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
11
by: Andy Fish | last post by:
Hi, I am trying to concoct a query that will join rows on the basis of a whitespace insensitive comparison. For instance if one row has the value 'a<space>b' and another has the value...
7
by: Linuxguy | last post by:
Hi guys I need your help.. I am trying to read a line from a file and remove whitespaces from it but it doesn't work.. thanks my code;
4
by: Michael Frost | last post by:
Loading and saving a xml document in .net creates whitespace in empty nodes! loading an empty node like: <node></node> ..net saves as: <node> </node>
20
by: ilyas | last post by:
I have some html which consists of a paragraph tag and some text inside it What I want to do is to maintain the whitespace in this paragrpah so that it appears exactly like that in the browser,...
25
by: Peter Michaux | last post by:
Hi, I'm thinking about code minimization. I can think of a few places where whitespace matters a + ++b a++ + b a - --b a-- -b when a line ends without a semi-colon in which case the new...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
2
by: Kaleb Pederson | last post by:
Hello, Does anybody know where in the standard it allows the preprocessed file to have whitespace removed/collapsed? I found the following in 16.1: "The source text is decomposed into...
3
by: joelkeepup | last post by:
Hi, im trying to create a text email message using xslt template , the transforms work great, but the newlines and whitespace in the xslt doc are removed. Is there a setting somewhere I have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.