我正在尝试将学生记录的数据保存到文本文件和二进制文件中,但输出不允许我输入任何数据,我找不到问题所在。有人可以帮忙吗?
我打开了一个文本文件和一个二进制文件,我使用了函数。 视觉工作室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));
}
答案 0 :(得分:1)
我认为您不能使用以下方式将结构写入文件:
outfilebin.write(reinterpret_cast<char *>(&student), sizeof(student));
它会写一些指针而不是字符串值;
另外,当你做
时outfile << student.firstname;
outfile << student.lastname;
它将输出所有这些字符串而没有分隔符,例如&#34; firstnamelastname15 ...&#34;你怎么期望读回来?
要将值写入二进制文件,请使用大小合适的结构,如
struct student {
char firstname[40];
char lastname[40];
int age;
}
您可以将整个结构读/写为二进制文件。
要将值写入文本文件,请使用分隔符或固定宽度:
fout << student.firstname << "\t" << student.lastname << "\t" << student.age << "\t" << std::endl;
要读取它们,请按行读取并将它们解析为结构(您可以在此处使用结构中的std字符串)。