How do I stop reading input at the end of a line?

W

W. Cerven

I have a list of n inputs of varying type I wish to read from a file:

For example:
string string int int double double int int int double ...

My input file gives its values on a single line, separated by spaces
or tabs.
However, only the first k<=n of these values are given. Without
knowing beforehand what k is, how do I read these k values to my
variables and stop reading when I reach the end of the line?

Thanks!
 
V

Victor Bazarov

W. Cerven said:
I have a list of n inputs of varying type I wish to read from a file:

For example:
string string int int double double int int int double ...

My input file gives its values on a single line, separated by spaces
or tabs.
However, only the first k<=n of these values are given. Without
knowing beforehand what k is, how do I read these k values to my
variables and stop reading when I reach the end of the line?

Read the entire line (using 'getline') into std::string object and then
parse it using std::istringstream.

V
 
E

E. Robert Tisdale

W. Cerven said:
I have a list of n inputs of varying type I wish to read from a file:

For example:
string string int int double double int int int double ...

My input file gives its values on a single line,
separated by spaces or tabs.
However, only the first k <= n of these values are given.
Without knowing beforehand what k is,
how do I read these k values to my variables and stop reading
when I reach the end of the line?

Why should you stop reading when you reach the end of the line?
The end-0f-line character [sequence] is just another whitespace
as far as C++ is concerned. It will find the k+1 value
at the beginning of the next line.
 
J

Jorge Rivera

W. Cerven said:
I have a list of n inputs of varying type I wish to read from a file:

For example:
string string int int double double int int int double ...

My input file gives its values on a single line, separated by spaces
or tabs.
However, only the first k<=n of these values are given. Without
knowing beforehand what k is, how do I read these k values to my
variables and stop reading when I reach the end of the line?

Thanks!

This will read every value into a string. It is up to you to convert it
to the right type.

If you know in advance what the type of your data is (fixed string
string int int....), you can use the same technique, substituting s1 for
the correct variable in which you want to store the values.

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::ifstream file( <full-path-her> );

std::string line;
std::istringstream parser;

if(file)
{
std::string value;
while( std::getline(file, line) )
{
parser.clear();
parser.str(line);

while(parser)
{
parser>>value;
std::cout<<"NextValue: "<<value<<std::endl;
}
}
}
return 0;
}
 
D

David Harmon

On Tue, 08 Jun 2004 14:20:12 -0700 in comp.lang.c++, "E. Robert Tisdale"
Why should you stop reading when you reach the end of the line?
The end-0f-line character [sequence] is just another whitespace
as far as C++ is concerned.

But apparently not as far as his desired input format is concerned.
Perhaps each line contains one set of data to be handles separately.

Perhaps some relative of
while (cin.peek() == ' ')
 
D

Default User

E. Robert Tisdale said:
Why should you stop reading when you reach the end of the line?
The end-0f-line character [sequence] is just another whitespace
as far as C++ is concerned. It will find the k+1 value
at the beginning of the next line.

And what if the end of a line denotes the completion of a record or
other processing iteration?



Brian Rodenborn
 
E

E. Robert Tisdale

Something said:
E. Robert Tisdale said:
Why should you stop reading when you reach the end of the line?
The end-0f-line character [sequence] is just another whitespace
as far as C++ is concerned. It will find the k+1 value
at the beginning of the next line.

And what if the end of a line denotes
the completion of a record or other processing iteration?

W. Cerven does may have meant but does not actually say that
"the end of a line denotes he completion of a record
or other processing iteration".

I have added you to my killfile twice now
but, unhappily, your trolls are still getting through.
Can anybody tell me how to get my Mozilla 1.6 newsreader
to killfile Default User once and for all?
 
W

W. Cerven

Thanks for the help!

In response to some of the earlier posts, yes, I am reading from a
file in which each line is a separate data record. However, the
latter inputs are optional and thus are not always provided. Thus, I
want to collect all of the data provided, but don't know exactly how
much is provided a priori.
 
T

Thomas Matthews

E. Robert Tisdale said:
Something said:
E. Robert Tisdale said:
Why should you stop reading when you reach the end of the line?
The end-0f-line character [sequence] is just another whitespace
as far as C++ is concerned. It will find the k+1 value
at the beginning of the next line.


And what if the end of a line denotes
the completion of a record or other processing iteration?


W. Cerven does may have meant but does not actually say that
"the end of a line denotes he completion of a record
or other processing iteration".

I have added you to my killfile twice now
but, unhappily, your trolls are still getting through.
Can anybody tell me how to get my Mozilla 1.6 newsreader
to killfile Default User once and for all?

Your definition of Troll needs redefining.
"Default User", Brian, was just pointing out an exception
or another case to test your assumption statement. He
was not trolling.

Also, I think you "This is obviously a troll, please ignore"
needs some refining, as some non-troll posts have been
erronously labelled.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
T

Thomas Matthews

W. Cerven said:
Thanks for the help!

In response to some of the earlier posts, yes, I am reading from a
file in which each line is a separate data record. However, the
latter inputs are optional and thus are not always provided. Thus, I
want to collect all of the data provided, but don't know exactly how
much is provided a priori.

Ahhh, optional fields and variable records.
As others have stated, I suggest that you read the line into
a string and then parse the string. Pass the string to functions
which extract their arguments. This is how I am currently parsing
my data {except I use an object for each field}.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
D

Default User

E. Robert Tisdale said:
Something that calls itself Default User wrote:

W. Cerven does may have meant but does not actually say that
"the end of a line denotes he completion of a record
or other processing iteration".

He didn't say he didn't either. You made the assumption that end of line
meant nothing, without asking.
I have added you to my killfile twice now
but, unhappily, your trolls are still getting through.
Can anybody tell me how to get my Mozilla 1.6 newsreader
to killfile Default User once and for all?

Trollsdale doesn't even remember that he KFed me in a different
newsgroup. Doesn't matter, I'll keep pointing out his bullshit
regardless. I don't care whether *he* sees my posts, it's the new and
untrained that can be mislead by his vicious misleading "information".




Brian Rodenborn
 
D

Default User

Thomas said:
Your definition of Troll needs redefining.
"Default User", Brian, was just pointing out an exception
or another case to test your assumption statement. He
was not trolling.

It's usually considered an honor for Trollsdale to label you a Troll.
You guys should check out his latest shenanigans on comp.lang.c.
Also, I think you "This is obviously a troll, please ignore"
needs some refining, as some non-troll posts have been
erronously labelled.

I'd say 90% of what he labels as a troll post is in fact one that merely
disagrees with him.



Brian Rodenborn
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top