使用C ++从CSV文件读取

时间:2019-06-25 12:43:01

标签: c++

我试图从CSV文件中读取记录。此代码是Froom GeeksforGeeks。我尝试编译它。然后,当我尝试运行时,它会终止并出现异常

void read_record() 
{ 

    // File pointer 
    fstream fin; 

    // Open an existing file 
    fin.open("reportcard.csv", ios::in); 

    // Get the roll number 
    // of which the data is required 
    int rollnum, roll2, count = 0; 
    cout << "Enter the roll number "
        << "of the student to display details: "; 
    cin >> rollnum; 

    // Read the Data from the file 
    // as String Vector 
    vector<string> row; 
    string line, word, temp; 

    while (fin >> temp) { 

        row.clear(); 

        // read an entire row and 
        // store it in a string variable 'line' 
        getline(fin, line); 

        // used for breaking words 
        stringstream s(line); 

        // read every column data of a row and 
        // store it in a string variable, 'word' 
        while (getline(s, word, ',')) { 

            // add all the column data 
            // of a row to a vector 
            row.push_back(word); 
        } 

        // convert string to integer for comparision 
        roll2 = stoi(row[0]); 

        // Compare the roll number 
        if (roll2 == rollnum) { 

            // Print the found data 
            count = 1; 
            cout << "Details of Roll " << row[0] << " : \n"; 
            cout << "Name: " << row[1] << "\n"; 
            cout << "Maths: " << row[2] << "\n"; 
            cout << "Physics: " << row[3] << "\n"; 
            cout << "Chemistry: " << row[4] << "\n"; 
            cout << "Biology: " << row[5] << "\n"; 
            break; 
        } 
    } 
    if (count == 0) 
        cout << "Record not found\n"; 
} 

程序以一条消息终止

  抛出'std :: invalid_argument'实例后调用

terminate     what():stoi“

2 个答案:

答案 0 :(得分:0)

您当前的错误是row [0]不是字符串。 stoi需要一个字符串。无效参数表示您在圆括号内赋予函数的值不是计算机期望的值。我可能会尝试打印一行的变量并使用

检查类型

cout << typeid(row[0]).name.()

最好的做法是在编码时逐步执行操作(在运行之前先走动。)尝试读入一个单元格,然后在遇到困难时关闭。然后一行。然后多行。它很容易注释掉代码行。许多文本编辑器甚至都有一个热键来注释掉代码行。尝试谷歌搜索

block comment hotkey [your text editor name]

答案 1 :(得分:0)

无效的read_record() {

// File pointer
fstream fin;

// Open an existing file
fin.open("reportcard.csv", ios::in);

// Get the roll number
// of which the data is required
int rollnum, count = 0;
cout << "Enter the roll number "
    << "of the student to display details: ";
cin >> rollnum;

ostringstream roll2;
roll2 << rollnum;
string roll = roll2.str();
//cout << roll;

// Read the Data from the file
// as String Vector
vector<string> row;
string line, word, temp;

while (!fin.eof()) {

    row.clear();

    // read an entire row and
    // store it in a string variable 'line'
    getline(fin, line);

    // used for breaking words
    stringstream s(line);

    // read every column data of a row and
    // store it in a string variable, 'word'
    while (getline(s, word, ',')) {

        // add all the column data
        // of a row to a vector
        row.push_back(word);
    }

    // convert string to integer for comparision
    //roll2 = stoi(row[0]);
    //cout << row[0] << row[1] << row[2] << row[3] << row[4] << row[5] << "\n";


    // Compare the roll number
    if (roll == row[0]) {

        // Print the found data
        count = 1;
        cout << "Details of Roll " << row[0] << " : \n";
        cout << "Name: " << row[1] << "\n";
        cout << "Maths: " << row[2] << "\n";
        cout << "Physics: " << row[3] << "\n";
        cout << "Chemistry: " << row[4] << "\n";
        cout << "Biology: " << row[5] << "\n";
        break;
    }
}
if (count == 0)
    cout << "Record not found\n";

}