Connecting Tech Pros Worldwide Forums | Help | Site Map

Stream input, critique my code?

Donald Canton
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

My goal is to read a text file consisting of lines containing
someone's name followed by one or more numeric values. All fields are
tab-delimited. The name could be in any one of the following formats:

John Doe
J. Doe
John H. Doe

and will always be followed by at least one tab character. Here's the
code:

#include <fstream>
#include <istream>
#include <iostream>
#include <string>

int main()
{
std::ifstream inputfile("data.txt");
if( !inputfile ) {
std::cerr << "Can't open file: " << '\n';
return -1;
}
std::istream& input = inputfile;
std::string name;
double value;

while ( std::getline(input, name, '\t') )
{
//do something with name, then read values
while ( input >> value ) {
//do something with value
}
input.clear(); //end of values, reset stream
}
return 0; //end of input
}

Are there any pitfalls in doing it this way? Suggestions?

Thanks in advance,

Donald Canton

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Stream input, critique my code?



"Donald Canton" <dcanton@rocketmail.com> wrote in message
news:c260a017.0405031157.4e706a82@posting.google.c om...[color=blue]
> Hi,
>
> My goal is to read a text file consisting of lines containing
> someone's name followed by one or more numeric values. All fields are
> tab-delimited. The name could be in any one of the following formats:
>
> John Doe
> J. Doe
> John H. Doe
>
> and will always be followed by at least one tab character. Here's the
> code:
>
> #include <fstream>
> #include <istream>
> #include <iostream>
> #include <string>
>
> int main()
> {
> std::ifstream inputfile("data.txt");
> if( !inputfile ) {
> std::cerr << "Can't open file: " << '\n';
> return -1;
> }
> std::istream& input = inputfile;[/color]

Why the above? Just use inputfile directly.
[color=blue]
> std::string name;
> double value;
>
> while ( std::getline(input, name, '\t') )
> {
> //do something with name, then read values
> while ( input >> value ) {
> //do something with value
> }
> input.clear(); //end of values, reset stream
> }
> return 0; //end of input
> }
>
> Are there any pitfalls in doing it this way? Suggestions?[/color]

It seems to work, but its tricky code that relies on the details on the
iostream library and the format of your input file. For instance it will be
defeated by a name that begins with a digit. You might think that will never
happen, but I know from experience you get all sorts of strange things in
large data sets (I remember the occasion when my code was defeated by
someone who had a superscripted digit in the middle of their name).

I would rather see code that reflects your requirements directly. I.e.

while (read a line)
{
split line at first tab into 'name' and 'values'
get name from 'name' (trim whitespace?)
create stringstream from 'values'
while (read a value)
{
do something with value
}
}

It might be more long winded but its a simple translation of your
requirements into code, with no trickery.

john


Donald Canton
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Stream input, critique my code?


"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<c76b5n$94je$1@ID-196037.news.uni-berlin.de>...[color=blue]
> "Donald Canton" <dcanton@rocketmail.com> wrote in message
> news:c260a017.0405031157.4e706a82@posting.google.c om...[color=green]
> > Hi,
> >
> > My goal is to read a text file consisting of lines containing
> > someone's name followed by one or more numeric values. All fields are
> > tab-delimited. The name could be in any one of the following formats:
> >
> > John Doe
> > J. Doe
> > John H. Doe
> >
> > and will always be followed by at least one tab character. Here's the
> > code:
> >
> > #include <fstream>
> > #include <istream>
> > #include <iostream>
> > #include <string>
> >
> > int main()
> > {
> > std::ifstream inputfile("data.txt");
> > if( !inputfile ) {
> > std::cerr << "Can't open file: " << '\n';
> > return -1;
> > }
> > std::istream& input = inputfile;[/color]
>
> Why the above? Just use inputfile directly.
>[color=green]
> > std::string name;
> > double value;
> >
> > while ( std::getline(input, name, '\t') )
> > {
> > //do something with name, then read values
> > while ( input >> value ) {
> > //do something with value
> > }
> > input.clear(); //end of values, reset stream
> > }[/color]
> return 0; //end of input[color=green]
> > }
> >
> > Are there any pitfalls in doing it this way? Suggestions?[/color]
>
> It seems to work, but its tricky code that relies on the details on the
> iostream library and the format of your input file. For instance it will be
> defeated by a name that begins with a digit. You might think that will never
> happen, but I know from experience you get all sorts of strange things in
> large data sets (I remember the occasion when my code was defeated by
> someone who had a superscripted digit in the middle of their name).
>
> I would rather see code that reflects your requirements directly. I.e.
>
> while (read a line)
> {
> split line at first tab into 'name' and 'values'
> get name from 'name' (trim whitespace?)
> create stringstream from 'values'
> while (read a value)
> {
> do something with value
> }
> }
>
> It might be more long winded but its a simple translation of your
> requirements into code, with no trickery.
>
> john[/color]

Thanks. I've made some changes per your recommendations.

dc
Closed Thread


Similar C / C++ bytes