学生记录

Students' records

本文关键字:记录      更新时间:2023-10-16

我试图将学生记录的数据保存到文本文件和二进制文件中,但是输出不允许我输入任何数据,我找不到哪里有问题。有人可以帮忙吗?

我打开了一个文本文件和二进制文件,然后使用了功能。Visual Studio 13没有向我显示任何错误。

    #include <iostream> 
    #include <fstream>
    #include <string>
    struct dof { int DD, MM, YYYY; };
    struct record
    {
        string firstname;
        string lastname;
        string adress;
        string highschooldegree;
        int phonenumber;
        int ID;
        int group;
        int coursescore[5];
        char grade;
        float GPA;
        dof birthday;
    }student;
void getdata(ifstream &, record &student);
void getdatabin(ifstream &, record &student);
void outputdata(ofstream &, record &student);
void outputdatabin(ofstream &, record &student);
int main()
{
    record student;
    ifstream infile;
    ifstream infilebin;
    ofstream outfile;
    ofstream outfilebin;
    infile.open("student.txt", ios::in | ios::app);
    if (infile.good())
        getdata(infile, student);
    else
        cout << "File cant be opened " << endl;
    infile.close();
    outfile.open("student.txt", ios::out);
    if (outfile.good())
        outputdata(outfile, student);
    else
        cout << "File cant be opened " << endl;
    outfile.close();
    infilebin.open("student.bin", ios::binary | ios::in | ios::app);
    if (infilebin.good())
        getdatabin(infilebin, student);
    else
        cout << "File cant be opened " << endl;
    infilebin.close();
    outfilebin.open("student.bin", ios::binary | ios::out);
    if (outfilebin.good())
        outputdatabin(outfilebin, student);
    else
        cout << "File cant be opened " << endl;
    outfilebin.close();
    return 0;
}
void getdata(ifstream &infile, record &student)
{
    for (int i = 0; i < 3; i++)
    {
        infile >> student.firstname;
        infile >> student.lastname;
        infile >> student.adress;
        infile >> student.highschooldegree;
        infile >> student.phonenumber;
        infile >> student.ID;
        infile >> student.group;
        for (int j = 0; j < 3; j++)
        {
            infile >> student.coursescore[j];
        }
        infile >> student.grade;
        infile >> student.GPA;
        infile >> student.birthday.DD >> student.birthday.MM >> student.birthday.YYYY;
    }
}
void getdatabin(ifstream &infilebin, record &student)
{
    infilebin.read(reinterpret_cast<char *>(&student), sizeof(student));
}
void outputdata(ofstream &outfile, record &student)
{
    outfile << student.firstname;
    outfile << student.lastname;
    outfile << student.adress;
    outfile << student.highschooldegree;
    outfile << student.phonenumber;
    outfile << student.ID;
    outfile << student.group;
    for (int j = 0; j < 3; j++)
    {
        outfile << student.coursescore[j];
    }
    outfile << student.grade;
    outfile << student.GPA;
    outfile << student.birthday.DD << student.birthday.MM << student.birthday.YYYY;
}
void outputdatabin(ofstream &outfilebin, record &student)
{
    outfilebin.write(reinterpret_cast<char *>(&student), sizeof(student));
}

我认为您不能使用以下方式编写要文件的结构:

outfilebin.write(reinterpret_cast<char *>(&student), sizeof(student));

它将写一些指针,而不是字符串值;

另外,当您做

outfile << student.firstname;
outfile << student.lastname;

它将没有定界符,例如" FirstNamelastName15 ..."之类的所有这些字符串,您如何期望将它们读回?

要将值写入二进制文件,请使用固定大小的结构,例如

struct student {
  char firstname[40];
  char lastname[40];
  int age;
}

您可以将整个结构读取到二进制文件。

要将值写入文本文件,使用定界符或固定宽度:

fout << student.firstname << "t" << student.lastname << "t" << student.age << "t" << std::endl;

要读取它们,用线条阅读并将它们解析为结构(您可以在此处使用结构中的std字符串)。