D
Donald Canton
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
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