G
Grumble
Hello all,
[ Disclaimer: I am a complete C++ newbie ]
I want to read lines from a text file, where each line has the
following syntax:
token1:token2:token3
There could be white space between tokens and ':'
There could be white space before token1 or after token3.
Because I will need to access every line several times, later in my
program, I first store every line in a string vector:
// Do you guys put the & near the type or near the parameter name?
static void read_lines(vector<string> &v)
{
ifstream ifs(INFILE); // input file stream
if (ifs == NULL)
{
cerr << "Unable to open input file " << INFILE << ".\n";
exit(-1);
}
string line;
while (getline(ifs, line))
{
// Ignore empty lines and comments.
if (line.empty() || line[0]==HASH) continue;
v.push_back(line);
}
}
Does that part look OK?
Later on, when I am dealing with a specific line, I create a
stringstream object so I can use the >> operator.
Ideally, I would simply write:
{
istringstream myss(mystring);
string token1, token2, token3;
myss >> token1;
myss >> token2;
myss >> token3;
}
But this doesn't work because ':' is not treated as white space. Is
there a simple solution?
Is my approach completely wrong?
Nudge
[ Disclaimer: I am a complete C++ newbie ]
I want to read lines from a text file, where each line has the
following syntax:
token1:token2:token3
There could be white space between tokens and ':'
There could be white space before token1 or after token3.
Because I will need to access every line several times, later in my
program, I first store every line in a string vector:
// Do you guys put the & near the type or near the parameter name?
static void read_lines(vector<string> &v)
{
ifstream ifs(INFILE); // input file stream
if (ifs == NULL)
{
cerr << "Unable to open input file " << INFILE << ".\n";
exit(-1);
}
string line;
while (getline(ifs, line))
{
// Ignore empty lines and comments.
if (line.empty() || line[0]==HASH) continue;
v.push_back(line);
}
}
Does that part look OK?
Later on, when I am dealing with a specific line, I create a
stringstream object so I can use the >> operator.
Ideally, I would simply write:
{
istringstream myss(mystring);
string token1, token2, token3;
myss >> token1;
myss >> token2;
myss >> token3;
}
But this doesn't work because ':' is not treated as white space. Is
there a simple solution?
Is my approach completely wrong?
Nudge