【问题标题】:I keep getting the 'std::invalid_argument' what(): stoi exception thrown for seemingly no reason?我不断收到 'std::invalid_argument' what(): stoi 异常抛出似乎没有理由?
【发布时间】:2019-12-09 04:59:43
【问题描述】:

我正在尝试读取以下格式的文件:1,2,4,6,8,12,12, .我想使用 getline() 和 stringstream 对象来分隔输入,并在转换为整数后将其存储到向量中。它可以工作,我可以看到输出能够添加数字一,但在完成转换文件中的所有数字后它仍然抛出异常。

输出: 2 3 5 7 9 13 13

#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

int main (int argc, char** argv){
    ifstream infile;
    vector <int> vect;
    infile.open("tested");
    if(!infile.is_open()){
        cout<<"File did not open"<<endl;
    }
    else{
        while(!infile.eof()) {
            string line;
            getline(infile, line);
            stringstream ss(line);
            while (ss){
                string p;
                getline(ss, p, ',');
                int x = stoi(p);
                cout<<x+1<<endl;
                vect.push_back(x);
            }
        }
        int i=0;
        while(i<vect.size()){
            int e = vect[i];
            cout<<e<<endl;
            i++;
        }
        sort(vect.begin(), vect.end());
        int j=0;
        while(j<vect.size()){
            int n = vect[j];
            cout<<n<<endl;
            j++;
        }
        cout<<"end reached"<<endl;
    }
}

【问题讨论】:

  • linegetline() 之后为空时会发生什么?
  • @SidS 据我了解,它只会调用 getline 一次,因为它会跳出 while 循环,因为我们已经到达文件末尾,这意味着 (!infile_.eof()) 条件会遇到的。
  • 您是否有任何迹象表明该假设是正确的?依赖eof() 可能是您问题的主要部分。尝试检查来自getline() 的返回值。
  • 另外,测试infile,而不是!infile.eof()
  • @SidS 我试过 infile,它仍然抛出异常,我不明白你检查 getline 的返回值是什么意思?

标签: c++ string vector file-io stringstream


【解决方案1】:

您的代码比它需要的更复杂。你根本不需要使用std::stoi()。由于您已经在使用std::stringstream,所以让它为您解析整数。

#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

int main (){
    ifstream infile("tested");
    if (!infile.is_open()){
        cout << "File did not open" << endl;
    }
    else{
        vector<int> vect;
        string line;
        while (getline(infile, line)) {
            istringstream iss(line);
            int x; char c;
            while (iss >> x) {
                cout << x + 1 << endl;
                vect.push_back(x);
                iss >> c;
            }
        }
        for (size_t i = 0; i < vect.size(); ++i){
            cout << vect[i] << endl;
        }
        sort(vect.begin(), vect.end());
        for (int j = 0; j < vect.size(); ++j){
            cout << vect[j] << endl;
        }
        cout << "end reached" << endl;
    }
    return 0;
}

Live Demo

【讨论】:

    【解决方案2】:

    关于我的评论,您在getline() 中使用了分隔符,所以当您的最后一个数字没有行时会引发异常。因为std::stoi 没有转换任何内容。

    所以我只是在getline() 为真时循环。

    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <sstream>
    #include <algorithm>
    
    int main (int argc, char **argv)
    {
      std::ifstream infile;
      std::vector<int> vect;
      infile.open ("tested", std::ios::in);
      if (!infile.is_open ())
        {
          std::cout << "File did not open" << std::endl;
        }
      else
        {
          std::string p;
          while (!infile.eof ())
            {
              std::string line;
              getline (infile, line);
              std::stringstream ss (line);
              // Changed
              while (getline (ss, p, ','))
                {
                  int x = stoi (p);
                  // std::cout << x + 1 << std::endl;
                  vect.push_back (x);
                }
            }
          int i = 0;
          while (i < vect.size ())
            {
              int e = vect[i];
              std::cout << e << std::endl;
              i++;
            }
          sort (vect.begin (), vect.end ());
          int j = 0;
          while (j < vect.size ())
            {
              int n = vect[j];
              std::cout << n << std::endl;
              j++;
            }
          std::cout << "end reached" << std::endl;
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    相关资源
    最近更新 更多