Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do I stop reading input at the end of a line?

Reply
Thread Tools

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

 
 
W. Cerven
Guest
Posts: n/a
 
      06-08-2004
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!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-08-2004
W. Cerven wrote:
> 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
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-08-2004
W. Cerven wrote:

> 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.
 
Reply With Quote
 
Jorge Rivera
Guest
Posts: n/a
 
      06-08-2004
W. Cerven wrote:
> 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;
}
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      06-08-2004
On Tue, 08 Jun 2004 14:20:12 -0700 in comp.lang.c++, "E. Robert Tisdale"
<(E-Mail Removed)> wrote,
>
>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() == ' ')

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-08-2004
"E. Robert Tisdale" wrote:

> 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
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-08-2004
Something that calls itself Default User wrote:

> E. Robert Tisdale wrote:
>
>>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?
 
Reply With Quote
 
W. Cerven
Guest
Posts: n/a
 
      06-09-2004
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.
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-09-2004
E. Robert Tisdale wrote:

> Something that calls itself Default User wrote:
>
>> E. Robert Tisdale wrote:
>>
>>> 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

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-09-2004
W. Cerven wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to stop java service with command line parameter '-stop'? Will Java 1 11-02-2004 03:32 PM
How to stop a thread without using stop() Son KwonNam Java 11 04-09-2004 08:01 PM
Q. My browser doesn't stop, when I click on STOP. Jim Jones Computer Support 7 02-03-2004 09:47 PM
Stop Debugging doesn't stop in ASP.NET Matt Theule ASP .Net 7 07-24-2003 07:38 PM



Advertisments