I would like to read input data from text file with fstream.
But I don't know how to mix different type of get functions.
I would like to read for example numbers after char "="
I try to use something like this

char c;
double x;    
while (!inFile.eof())
{
  inFile >> c;
  if (c == '=') inFile >> x;
}

But its wrong, and also didnt recognize eof.

I also would like to skip comments started with "#"
therefore I want to throw away lines,
but getline() does not work with no arguments

Recommended Answers

All 6 Replies

use getline() to read the file line by line, then parse the string

>>getline() does not work with no arguments
Of course not -- you have to pass appropriate argument

std::string line;
ifstream inFile("filename.txt");
while( getline(inFile, line) )
{
   // blabla
}

But I dont want to use string manipulation, if it is possible,
because its very simple, and I want to read numbers,

But I dont want to use string manipulation, if it is possible,
because its very simple, and I want to read numbers,

What's the format of your file?

I dont understand whats wrong with the above piece of code. Considering that there is a number for sure after the '=' . it should work.

My format is for example:
#comment
x1=1 y1=2 z1=3
x2=1 y2=4 z2=9
#EOF

I need 1,2,3 and 1,4,9
With the >> operator
I cannot get char and double char and double after each other,
as I see.

There are a million and one ways to parse a string. Here's one idea that might get your creative juices flowing:

while (getline(in, line))
{
    // Skip comments
    if (line[0] == '#')
    {
        continue;
    }

    istringstream split(line);
    string temp;

    while (getline(split, temp, '='))
    {
        int value;

        split>> value;
        cout<< value <<'\n';
    }
}
commented: My thoughts too :) +28
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.