What is an elegant way (using std::stream's) to extract number of
white-space separated entries in a given line of a formatted text file?
e.g.: Take this section of a file,
1 2.78 4 5 -0.003 <tab> 7.1d5
9 40 <tab> 2.e5 -10
....
and the code will return 6 for 1st line, 4 for next line, etc...
- slyi 12 2364
levent wrote: What is an elegant way (using std::stream's) to extract number of white-space separated entries in a given line of a formatted text file?
e.g.: Take this section of a file,
1 2.78 4 5 -0.003 <tab> 7.1d5 9 40 <tab> 2.e5 -10 ...
and the code will return 6 for 1st line, 4 for next line, etc...
Read the line using 'std::getline'.
Define 'std::ostringstream' from the string you just read.
Read fields as 'std::string' objects until the end of the string stream.
V
Victor, Did you mean to say std::istringstream?
string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, ' ');
}
Victor, I forgot to cut and paste the closing brace.
string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, ' ');
}
Victor, You are right. You don't even need the iss.ignore:
string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
}
Thx for the answers. They have some issues though:
- in order to extract the whole line into a string we need some sort of
prespecified (or pre-detected) limit for # of char's per line
(arguably, not elegant).
- the question is not to extract the entries themselves. It is to
extract the number of entries.
I meant something more compact such as:
ifstream file("thefile");
double tmp;
int nCol;
// effectively, the task takes just one line:
for(nCol=0; _pred_ ; nCol++) file >> tmp;
where _pred_ is such that it returns false when end of *line* is
reached, or smth like that.
Frank Chang wrote: Victor, You are right. You don't even need the iss.ignore:
string line; istringstream iss(line); while (!iss.eof()) { iss >> word; cout << word << endl; }
while (iss >> word)
cout << word << '\n';
But even then, it doesn't solve the original problem.
int count = 0;
while (iss >> word)
++count;
cout << count << '\n';
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Yes, I know you wanted the count , not the entries themselves. I just
took the code from an application I wrote just to illustrate the
general idea. - in order to extract the whole line into a string we need some sort of prespecified (or pre-detected) limit for # of char's per line (arguably, not elegant).
Could you please tell me why this is true? As Victor said, std::getline
will handle a line of arbitrary length.
that's right. I was confused with istream::getline( ).
the answer was actually trivial, as the general idea you were trying to
point out.
thanks all
- slyi
slyi, Actually the std::istringstream class can handle more than
white-space character delimiters, as my earlier example attempted to
show:
istringstream iss(line);
while (iss)
{
iss >> word;
iss.ignore(1, ' '); // the delimiter does not have to be white space
}
The ignore member function takes two arguments, the first is the number
of characters to be extracted and ignored and the second is the
delimiter character. In effect, the class istringstream gives you an
elegant tokenizer DFA for free so that you don't have to write your own
tokenizer C++ class.
Frank Chang wrote: Yes, I know you wanted the count , not the entries themselves. I just took the code from an application I wrote just to illustrate the general idea.
- in order to extract the whole line into a string we need some sort of prespecified (or pre-detected) limit for # of char's per line (arguably, not elegant).
It looks like you want to quote text using the broken Google interface,
but don't know how. To do so, DON'T use the Reply at the bottom of the
message. Click "show options" and use the Reply in the expanded header.
That will give you proper quotes and attributions.
This has been a public service announcement.
Brian
This is a test.
Default User wrote: Frank Chang wrote:
Yes, I know you wanted the count , not the entries themselves. I just took the code from an application I wrote just to illustrate the general idea.
> - in order to extract the whole line into a string we need some > sort of prespecified (or pre-detected) limit for # of char's per > line (arguably, not elegant).
It looks like you want to quote text using the broken Google interface, but don't know how. To do so, DON'T use the Reply at the bottom of the message. Click "show options" and use the Reply in the expanded header. That will give you proper quotes and attributions.
This has been a public service announcement. Brian
Frank Chang wrote: This is a test. Default User wrote: Frank Chang wrote:
This has been a public service announcement.
Ok, but don't top-post when doing it for real.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ren |
last post by:
Suppose I have a file containing several lines similar to this:
:10000000E7280530AC00A530AD00AD0B0528AC0BE2
The data I want to extract are 8 hexadecimal strings, the first of
which is E728,...
|
by: Sharon |
last post by:
hi,
I want to extract a string from a file,
if the file is like this:
1 This is the string 2 3 4
how could I extract the string, starting from the 10th position (i.e. "T")
and...
|
by: klall |
last post by:
Hello.
I need to extract date information from a memo field entered in the
following way: 01/01/2005 - 31/12/2005 01/01/2004 - 31/12/2004
01/01/2003 - 31/12/2003 01/01/1996 - 31/12/1996. The...
|
by: Elric02 |
last post by:
I'm currently trying to get access to the Python source code, however
whenever I try to extract the files using the latest version of WinZip
(version 10) I get the following error "error reading...
|
by: Fabian Braennstroem |
last post by:
Hi,
I would like to remove certain lines from a log files. I had
some sed/awk scripts for this, but now, I want to use python
with its re module for this task.
Actually, I have two different...
|
by: napolpie |
last post by:
DISCUSSION IN
USER nappie writes:
Hello, I'm Peter and I'm new in python codying and I'm using parsying
to extract data from one meteo Arpege file. This file is long file and
it's composed by...
|
by: Fabian Braennstroem |
last post by:
Hi,
I would like to delete a region on a log file which has this
kind of structure:
#------flutest------------------------------------------------------------
498 1.0086e-03 2.4608e-04...
|
by: beacon |
last post by:
Hey everybody,
I'm sure this is cake, but I'm still kinda stumped. I'm trying to summarize info on a report based on the number of entries on the report.
I have 5 fields: FName, LName,...
|
by: Steve |
last post by:
Hi all
Does anybody please know a way to extract an Image from a pdf file and save
it as a TIFF?
I have used a scanner to scan documents which are then placed on a server,
but I need to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |