473,799 Members | 3,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extract number of entries in a line

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

Aug 18 '05 #1
12 2403
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::ostringst ream' from the string you just read.
Read fields as 'std::string' objects until the end of the string stream.

V
Aug 18 '05 #2
Victor, Did you mean to say std::istringstr eam?

string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, ' ');
}

Aug 18 '05 #3
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, ' ');
}

Aug 18 '05 #4
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;
}

Aug 18 '05 #5
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.

Aug 18 '05 #6
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)
Aug 19 '05 #7
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
prespecifie d (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.

Aug 19 '05 #8
that's right. I was confused with istream::getlin e( ).

the answer was actually trivial, as the general idea you were trying to
point out.

thanks all

- slyi

Aug 19 '05 #9
slyi, Actually the std::istringstr eam 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.

Aug 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3176
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, like this: :10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2 Also, the bytes in the string are reversed. The E728 needs to be 28E7,
9
16987
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 extract 35 characters (including "T") from a file and then go to next line?
5
1987
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 number of entries varies. I would welcome advice on a more elegant way of extracting all the date information (first date, second date, third date, etc and appending to another table, say tblDates) rather than the crude way I have used left
2
355
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 however after processing 0 entries
8
2844
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 log files. The first file looks like: ...
0
2054
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
2
3103
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 9.8589e-05 1.4908e-04 8.3956e-04 3.8560e-03 4.8384e-02 11:40:01 499 499 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04
3
1735
beacon
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, Program, SkillLevel, and PhoneNumber. Each of these fields make up one line of info on the report. I want to count the number of entries and summarize it at the end of the report. Do I have to have some kind of number field, maybe a hidden one, associated...
5
5768
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 extract the image of the document (just the first page if there are multiple pages) and save it as a TIFF so I can then use the Tesseract OCR to get the text in the image.
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.