I wrote a little program to read soma data from a *.txt file.
The content of the file is "5 , 10, 15".

The program shall identify the numbers, e.g. "5" and "10" and "15".
This works until the last two numbers. Unfortunately the program doesn't read the last two numbers.

When I set a "," at the end of the file, "5, 10, 15," then it works.

I don't know what the reason for this behaviour is. Maybe someone could help me?

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

using namespace std;

int main()
    ifstream filestream("test.txt");
    string s;
    for(char ch;filestream.good();)
        if((ch>='0') && (ch<='9'))

          cout<<s<<" ";

    return 0;

To me your code seems ok logically. Just a small guess, are you sure you enter a newline in your text file at the end i.e. after 15. If not just give it a try. I am not sure this will work but can't think of anything else at this moment

Edited 4 Years Ago by DJSAN10: n/a

No, there is no newline. If I enter a newline at the end of textfile, it works as well as a ",".
But normally reading from a file should also work, when there is no newline or ",", shouldn't it?

Edited 4 Years Ago by Sunshine2011: n/a

This article has been dead for over six months. Start a new discussion instead.