0

The numbers I have to read from the file look like this: 1,5,26,3,86,35

I managed to read each number seperately, but my problem is with numbers that are more than one digit. eg 26 or 86.

How can I read them as one number instead of 2 6 and 8 6?

This is what I have sofar:

    int main()
{
    fstream numbers;
    string line;
    int num;
    
    numbers.open("test.txt");
    
    if(!(numbers))
        cout<<"error: file could not be read."<<endl;
    
    while(getline(numbers,line))
    {
        for(int i = 0; i<line.length();i++)
        {
            if(isdigit(line[i]))
                cout<<line[i]<<endl;
        }
    }
}

Thank you.

  • 1
    You search the internet for "c++ read file comma separated". Too many examples out there already. – Thomas Matthews Jul 02 '20 at 15:32
  • The easy (if a little slow) way is `getline` to get a line and place the line into a `istringstream` and then use `getline` on the `istringstream` to separate based on commas rather than newlines. Convert the resulting tokens according to your needs. I'm going to poke around a bit to see if I can find a duplicate. – user4581301 Jul 02 '20 at 15:32
  • 2
    This is good, but might be a little too general: https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c – user4581301 Jul 02 '20 at 15:34
  • 1
    Popping *read comma separated numbers in C++ site:stackoverflow.com* into Google will provide many alternatives. – user4581301 Jul 02 '20 at 15:37

2 Answers2

0

You can make it simpler with operator>>

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

int main() {
    std::fstream numbers("test.txt");
    int num;

    if(!(numbers)) std::cout<<"error: file could not be read.\n";
    
    while(numbers >> num) {
        std::cout<<num<<'\n';
        numbers.ignore();
    }
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • OP wants to read comma seperated numbers. The duplicates are about CSV. There is a big difference in complexity between the answers in this question and the answers in the dupes. Why should OP implement a CSV parser if it's possible to solve the problem with 15 lines of code? – Thomas Sablik Jul 02 '20 at 15:42
  • 1
    I added a 2 more dups that deals with comma separated numbers, and I like your answer so you get an upvote too. :) – Ted Lyngmo Jul 03 '20 at 06:40
0

This can be done using istringstream class. This is just an stream object like cin. You can use it in the following way:

int main(){
    string str = "10,20,30,40,50";
    istringstream iss(str);           // create an istringstream class object and pass 
                                      //    the string as argument.
    int k;
    while(iss >> k){
        cout << k << endl;
        if(iss.peek() == ',')                // check if the next character is a comma 
                                             // or any other delimiter and ignore it
            iss.ignore();
    }


    return 0;
}

It should work. This can be used to extract values from any string with any delimiter.

Madhur Vashistha
  • 329
  • 1
  • 2
  • 7