无法读取文件并存储在结构的数据成员中



这是针对赋值的,所以我遵循所需结构布局和重载的赋值指南。试图让我的程序读取每一行并将每一行分配给变量。

文本文件如下所示:

John 
S
Maggey
G

我的代码看起来像:

#include "iostream"
#include "fstream"
namespace
{
    char buffer[1024];
    int allocated = 0;
}
//====================================================================
struct student
{
    char *firstname;
    char lastname;
    int studentId;
    int occupied;
    student() : lastname(0), studentId(0), occupied(0)
    {
        firstname = new char[64];
        for (int i = 0; i < 64; ++i)
            firstname[i] = 0;
    }
    student(int s)
    {
        std::cout << "constructor" << std::endl;
        std::cout << "Allocated: " << allocated << std::endl;
        int currentLoc = allocated;
        allocated += s;
        firstname = new (&buffer[currentLoc]) char[s];
        for (int i = 0; i < 64; ++i)
            firstname[i] = 0;
    }
    void *operator new(size_t s)
    {
        std::cout << "Operator new allocated: " << allocated << std::endl;
        int currentLoc = allocated;
        allocated += s;
        return &buffer[currentLoc];
    }
    void student::operator delete(void *ptr)
    {
        std::cout << "Delete called " << std::endl;
        std::free(ptr);
    }
    student::~student()
    {
    }
};
//====================================================================
int main(int argc, char** argv)
{
    student *studentLoader = new student[25];
    std::fstream fin;
    fin.open("students.txt");
    char ln;
    for (int i = 0; i < 25; ++i)
    {
        fin.getline(studentLoader[i].firstname, 99);
        fin.getline(ln, 64);
        studentLoader[i].lastname = ln;
        studentLoader[i].studentId = (rand() % (9999 - 999)) + 999;
        studentLoader[i].occupied = 1;
        if ((i % 10) == 0)
        {
            std::cout << "First name: " << studentLoader[i].firstname << " Last initial: " << studentLoader[i].lastname << " ID: " << studentLoader[i].studentId << std::endl;
            delete[] studentLoader;
        }
    }
    fin.close();
    return 0;
}

我也尝试做:

    std::fstream fin;
    fin.open("students.txt");
    char ln;
    for (int i = 0; i < 25; ++i)
    {
        fin.getline(studentLoader[i].firstname, 99);
        fin.getline(studentLoader[i].lastname, 99);
        studentLoader[i].lastname = ln;
        studentLoader[i].studentId = (rand() % (9999 - 999)) + 999;
        studentLoader[i].occupied = 1;
        if ((i % 10) == 0)
        {
            std::cout << "First name: " << studentLoader[i].firstname << " Last initial: " << studentLoader[i].lastname << " ID: " << studentLoader[i].studentId << std::endl;
            delete[] studentLoader;
        }
    }
    fin.close();
    return 0;
}

但是第二次尝试获取线路时会收到错误。

尝试时

fin >> studentLoader[i].firstname;

不会将任何内容插入到变量中。

如果这不是一个错别字,并且你真的想在lastname中使用单个字符,你也应该把它作为一个字符来阅读: fin >> studentLoader[i].lastname >> std::ws .

此外,请考虑阅读您正在使用的函数和类的文档。这通常是个好习惯,可以使您免于大量失败。

通过将firstnamelastname的类型更改为std::string来稍微修改您的struct student,您不必动态分配/释放内存,您将代码减少一半。

例如,您可以将其定义为:

struct student {
    // constructor
    student (std::stirng fn, std::stirng ln, int ID, int oc) 
       :  firstname(fn), lastname(ln), studentID(ID), occupied(oc) { }
    // data members
    std::string firstname;
    std::string lastname;  
    int studentID;
    int occupied;
}; 

然后要从文件中读取并存储在struct student中,可以使用std::vector,如下所示:

#include <iostream>
#include <string>
#include <vector>
#include <sstream> 
int main () {
    // stores all elements created from the reading of the file 
    std::vector<student> class_of_students;
    // attach an input stream
    std::ifstream fin("students.txt");
    // check if file successfully opened 
    if (!fin) std::cerr << "Can't open input file!n";
    // line of text
    std::string line;
    // read file line by line
    while (getline(fin, line)) {
         // stream to extract data from a line
         std::stringstream ss(line);
         // input variables
         std::string first_name;
         std::string last_name;
         int ID;
         int occupied;
         // example that assumes line format: "f_name l_name ID occ"
         while (ss >> first_name >> last_name >> ID >> occupied) {
             // store one element of type student to the vector
             class_of_students.emplace_back(student(first_name, last_name, ID, occupied));
         }
    }
}

更多关于 std::string 和 std::vector 的使用。

注意:

此外,当您生成学生ID时,您不仅需要它是随机的,而且需要唯一的。

最新更新