简体   繁体   中英

How to read integer values separated by different delimiters on each line in a file?

I have a text file containing integer values separated on each line by a different character.

So the user enters the file name then a function that takes a file name is called and return the sum of all the integer values read.

On each line the user is prompted to input the separator.

If the file opening failed the function getSum() should report that through an error output stream object and returns -1 .

Here is what I've tried:

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


int getSum(std::string const& fileName){
    std::ifstream in(fileName);
    if(!in){
        std::cerr << "Unable to open input file!\n";
        return -1;
    }

    int sum = 0;
    for(std::string line; std::getline(in, line); ){
        std::cout << "enter separator: ";
        char separator;
        std::cin.get(separator);
        std::istringstream iss(line);
        for(std::string strVal; std::getline(iss, strVal, separator); ){
            sum += std::stoi(strVal);
        }
    }

    return sum;
}


int main(){

    std::string fileName;
    std::cout << "File name: ";
    std::getline(std::cin, fileName);
    std::cout << getSum(fileName) << '\n';


    std::cout << '\n';
}

The input file: data.txt:

25 20 16
7 0 3

Separator: ' '

The output:

68

Why it is not 71 as guessed?

And if I make char separator= ' '; and remove std::cin.get(separator); it gives me the correct output: 71 . So I guess the problem is with std::cin.get(separator) .

So is there a workaround?

Here is the problem:

int getSum(std::string const& fileName){
    std::ifstream in(fileName);
    if(!in){
        std::cerr << "Unable to open input file!\n";
        return -1;
    }

    int sum = 0;
    for(std::string line; std::getline(in, line); ){
        std::cout << "enter separator: ";
        char separator;
        std::cin.get(separator); //every time the for-loop loops, this happens again, resetting separator
        std::istringstream iss(line);
        for(std::string strVal; std::getline(iss, strVal, separator); ){
            sum += std::stoi(strVal);
        }
    }

    return sum;
}

Here is the fix:

int getSum(std::string const& fileName) {
    std::ifstream in(fileName);
    if (!in) {
        std::cerr << "Unable to open input file!\n";
        return -1;
    }

    int sum = 0;
    std::cout << "enter separator: ";
    char separator;
    separator = std::cin.get(); //get separator BEFORE the for-loop

    for (std::string line; std::getline(in, line); ) {
        std::istringstream iss(line);
        for (std::string strVal; std::getline(iss, strVal, separator); ) {
            sum += std::stoi(strVal);
        }
    }

    return sum;
}

It now outputted 71 as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM