为什么 getline() 在我的代码 (c++) 中不起作用

Why is getline() not working in my code (c++)?

本文关键字:c++ 不起作用 代码 getline 为什么 我的      更新时间:2023-10-16

我有类"消息",其中包括成员:

protected:
      int * next;
      int amount;
      std::string ** mes;
public:
      message(std::ifstream*);

构造函数的代码是:

message :: message(ifstream * myfile){
    *myfile >> amount;
    if (amount==0){
                next = new int[1];
                *myfile >> next[0];
                mes = new string*[1];
                getline(*myfile,*mes[0]);
                }
    else{
                next = new int[amount];
                mes = new string*[amount];
                for (int i=0;i<amount;i++){
                    *myfile >> next[i];
                    getline(*myfile,*mes[i]);
                    }
                }
    }

使用运算符>>从文件中读取工作正常,但是程序在getline()上崩溃 - 为什么?我应该更改什么?

您没有为要求std::getline()读取的std::string变量分配任何内存。 您正在分配不指向任何内容的string*指针数组。 您需要将代码更改为:

  1. 继续使用指针数组,但为它们分配实际的string变量:

    std::string ** mes;
    

    if (amount == 0) {
        ...
        mes = new string*[1];
        mes[0] = new string; // <-- here
        std::getline(*myfile, *mes[0]);
    }
    else {
        ...
        mes = new string*[amount];
        for (int i = 0; i < amount; ++i) {
            mes = new string[amount]; // <-- here
        }
        for (int i = 0; i < amount; ++i) {
            ...
            std::getline(*myfile, *mes[i]);
        }
    }
    
  2. 首先删除不必要的间接级别:

    std::string * mes;
    

    if (amount == 0) {
        ...
        mes = new string[1];
        std::getline(*myfile, mes[0]);
    }
    else{
        ...
        mes = new string[amount];
        for (int i = 0; i < amount; ++i) {
            ...
            std::getline(*myfile, mes[i]);
        }
    }
    

也就是说,您应该停止使用原始数组,而是使用 std::vector

#include <vector>
protected:
    std::vector<int> next;
    std::vector<std::string> mes;
    int amount;

message :: message(ifstream * myfile) {
    *myfile >> amount;
    if (amount == 0) {
        next.resize(1);
        mes.resize(1);
        *myfile >> next[0];
        std::getline(*myfile, mes[0]);
    }
    else {
        next.resize(amount);
        mes.resize(amount);
        for (int i = 0; i < amount; ++i) {
            *myfile >> next[i];
            std::getline(*myfile, mes[i]);
        }
    }
}

无论哪种方式,您都应该考虑摆脱amount == 0情况下的冗余代码。 使用局部变量,如果amount为 0,则将其设置为 1,否则将其设置为实际amount,然后您可以使用单个代码行来执行分配,而不考虑amount值:

message :: message(ifstream * myfile) {
    *myfile >> amount;
    int numElements = (amount == 0) ? 1 : amount;
    next = new int[numElements];
    mes = new string[numElements];
    for (int i = 0; i < numElements; ++i) {
        *myfile >> next[i];
        getline(*myfile, mes[i]);
    }
}

message :: message(ifstream * myfile) {
    *myfile >> amount;
    int numElements = (amount == 0) ? 1 : amount;
    next.reserve(numElements);
    mes.reserve(numElements);
    for (int i = 0; i < numElements; ++i) {
        int value;
        *myfile >> value;
        next.push_back(value);
        std::string line;
        std::getline(*myfile, line);
        mes.push_back(line);
    }
}