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

Dani AI

Generated

Short answer: stop trying to read a single char looking for = in a token like x1=1. The file you showed (lines like x1=1 y1=2 z1=3 and comment lines beginning with #) is easiest to handle by reading whitespace-separated tokens, skipping #-started lines, then splitting each token on = and converting the RHS to a number.

As described, the failing pattern comes from two things: while(!inFile.eof()) is unreliable, and inFile >> c will read the next non-whitespace character (for x1=1 that yields 'x', not '='). and are right that line-based parsing works; ’s istringstream approach is solid for full-line parsing. If you prefer not to do per-line string splitting, the token-based approach below keeps things simple and robust.

Example (compact, avoids per-line string parsing):

#include <fstream>
#include <string>
#include <limits>
#include <iostream>

std::ifstream in("data.txt");
std::string tok;
while (in >> tok) {
    if (tok.empty()) continue;
    if (tok[0] == '#') {
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        continue;
    }
    auto p = tok.find('=');
    if (p == std::string::npos) continue;
    std::string name = tok.substr(0, p);
    std::string valstr = tok.substr(p + 1);
    try {
        double v = std::stod(valstr);
        // use name and v
        std::cout << name << " => " << v << '\n';
    } catch (...) {
        // handle bad numeric text
    }
}

Quick tips: if your file sometimes has spaces around = (e.g. x1 = 1), handle both cases: check for a standalone token "=" (then read the next numeric token), and also handle name=value tokens as above. Always prefer while(in >> tok) or while(getline(...)) over while(!eof()), and guard numeric conversion with std::stod try/catch or strtod checks.

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.