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;
}

Dani AI

Generated

The symptom you described (works when you add a trailing comma or newline) is exactly the clue: the code only prints a number when it sees a non-digit delimiter, so a final number that ends the file without a delimiter never gets flushed. was right that adding a newline makes it work — but the real fix is to stop relying on a delimiter or on filestream.good() as the loop condition.

A simple, safe fix is to read characters with while (filestream.get(ch)), append digits to a buffer, flush that buffer when you see a non-digit, and after the loop flush any leftover buffer once more. Also use std::isdigit(static_cast<unsigned char>(ch)) to avoid UB on signed char.

Example (minimal fix):

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

int main() {
    std::ifstream filestream("test.txt");
    std::string s;
    char ch;
    while (filestream.get(ch)) {
        if (std::isdigit(static_cast<unsigned char>(ch)))
            s += ch;
        else if (!s.empty()) {
            std::cout << s << ' ';
            s.clear();
        }
    }
    if (!s.empty()) std::cout << s;
}

A different, often cleaner approach is to split on commas and parse tokens (this handles missing trailing delimiters naturally):

#include <sstream>
// ...
std::string token;
while (std::getline(filestream, token, ',')) {
    std::istringstream iss(token);
    int n;
    if (iss >> n) std::cout << n << ' ';
}

Avoid using filestream.good() or while (!filestream.eof()) as your primary loop condition; prefer checking the result of the extraction itself (see std::istream::get behavior). For reference, the get extraction semantics are documented at std::istream::get.

Recommended Answers

All 3 Replies

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

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?

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.