jeudi 27 octobre 2016

stoi being printed to console when using std::stoi

Given a file that contains one integer per line, I am trying to use this code to read the file and store the numbers in a vector.

Strangely, after finishing reading the file, my program is printing the string "stoi". Is this just a behavior of std::stoi? I couldn't find anything about this in documentation.

Here is the relevant code:

int main(int argc, char* argv[]) {
try{
     if (argc <= 1) {
         usage();
         return 1;
     }

     std::ifstream inputFile;
     inputFile.open(argv[1], std::ios::in);

     if (!inputFile.is_open()) throw std::runtime_error("Failed to open file");

     BinarySearchTree tree;
     std::string line;
     std::vector<int> nums;

     while(!inputFile.eof()) {
         getline(inputFile, line);
         int num = std::stoi(line);
         nums.push_back(num);
     }

     // Clean up
     inputFile.close();
     return 0;
}

catch(const std::exception& e) {
    std::cerr << e.what() << std::endl;
}
}

Aucun commentaire:

Enregistrer un commentaire