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();)
{
filestream.get(ch);
if((ch>='0') && (ch<='9'))
{
s+=ch;
}
else
{
cout<<s<<" ";
s.clear();
}
}
return 0;
}