【问题标题】:Reading from file C++从文件 C++ 中读取
【发布时间】:2014-04-02 11:58:22
【问题描述】:

我需要阅读这个 txt 文件,但我不明白我做错了什么? http://pastebin.com/gPVbuvhb这是我的阅读功能

void Read(School & snew){
    string school, name, group;
    Student S;
    ifstream fd(CD);
    getline(fd, school, ';');

    S.SetSchool(school);
    cout << S.GetSchool() << endl;
    while(!fd.eof()){
        getline(fd, name, ',');
        fd >> ws;
        getline(fd, group, ' ');
        int Paz[Student::M];
        int kiek = 0;
        while(fd.peek()!= '\n' && !fd.eof()){
            fd >> Paz[kiek++];
        }
        fd.ignore();
    }
    fd.close();
}

这是我的学生课

class Student{
public:
    static const int M = 5;
private:
    string school, name, group;
    int *Marks; // dynamic array of student marks
    int nmax; // max size of array
    int n; // current size of array
    void IncreaseCapasity(int kiek);
public:
    Student(int nmax = 0);
    ~Student();

    void SetMarks(int mark);
    void SetSchool(string school);
    void SetName(string name);
    void SetGroup(string group);

    int GetMark(int i){return Marks[i];}
    string GetSchool(){return school;}
    string GetName(){return name;}
    string GetGroup(){return group;}
    int GetN(){return n;}
};

Student::Student(int nmax):Marks(NULL), n(n), nmax(nmax){
    if(nmax > 0){
        Marks = new int[nmax];
    }
}

Student::~Student(){
    if(Marks){
        delete [] Marks;
        Marks = NULL;
    }
}

void Student::IncreaseCapasity(int kiek){ // maybe this function is incorrect?
    if(kiek > nmax){ // if array increasing
        int *SNew = new int [kiek];
        for(int i=0; i<n; i++)
            SNew[i] = Marks[i];
        delete [] Marks;
        Marks = SNew;
        nmax = kiek;
    }if(kiek < nmax){ // if array decreasing
        int *SNew = new int [kiek];
        for(int i=0; i<kiek; i++)
            SNew[i] = Marks[i];
        delete [] Marks;
        Marks = SNew;
        n = nmax = kiek;
    }
}

void Student::SetMarks(int mark){
    if(n == nmax) IncreaseCapasity(n + M);
    Marks[n] = mark;
    n++;
}

void Student::SetSchool(string school){
    this->school = school;
}

void Student::SetName(string name){
    this->name = name;
}

void Student::SetGroup(string group){
    this->group = group;
}

当我读取 int 值 fd >> Paz[kiek++];我收到这个错误 ConsoleApplication1.exe 中 0x571121F8​​ (msvcp110d.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0x0000000D。

【问题讨论】:

  • 如果您遵循“正常”的编码约定,我们都会发现您的代码更容易阅读。使用描述性变量名称并以小写字母开头。例如student 而不是 S。另外:CD 是什么?
  • 一方面,您正在检查您的 IO 操作中的 0 个操作是否成功。 And your usage of eof() as your loop conditional is wrong.
  • 您将getline';' 的分隔符参数一起使用。文件中没有; 字符,因此第一个getline 将其视为一个大行并全部读取。后续getlines 什么都没有了。
  • 谢谢!现在工作:)
  • 我刚刚编辑了我的代码。我不明白为什么会出现访问冲突读取位置错误。我认为我的功能增加容量是错误的(但我不确定)。我检查了所有代码,但仍然无法理解为什么会出现这个愚蠢的错误?

标签: c++


【解决方案1】:
getline(fd, school, ';');

fd 流中读取,在第一次出现';' 时停止。由于您的文件中没有';',它会将整个文件读入school 字符串。

你真正想做的是逐行解析你的文件,使用每一行构造istringstream的实例:

std::string line;
while (std::getline(fd, line)) {

    if (line.empty())
        continue;                      

    std::istringstream is(line);
    std::string name, group;
    if (std::getline(is, name, ',') && std::getline(is, group, ',')) {
        std::cout << "Name: " << name << " Group: " << group << std::endl;
    }

}

别忘了#include &lt;sstream&gt;


还要注意:

while (!fd.eof()) {
    std::getline(...);
    // relying on getline call being successful here
}

不安全,直接使用它的返回值。

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 2016-06-18
    • 2016-08-02
    • 2020-11-28
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多