学生记录
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字符串)。
相关文章:
- 将两个数组中的差异记录在第三个数组中
- 禁止在控制台上记录谷神星
- 创建 Spdlog 异步文件记录器时遇到困难
- fastrtps:如何在发布/订阅级别使用 DDS 历史记录?
- 如何将自定义记录器与websocketpp一起使用?
- C++结构到德尔福记录dll调用
- 如何判断SSL_read是否已经接收并处理了来自单个消息的所有记录
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 使用 Doxygen 记录枚举类值,而不启用EXTRACT_ALL
- 在C++中隐藏键盘记录器的控制台窗口
- 堆栈上的最大激活记录数
- 记录Doxygen中的命名
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 在SQLITE数据库中写入记录需要花费大量时间.如何提高刀片操作效率?
- Qt 错误:QSqlQuery::value:尝试从表中检索统计信息时未定位在有效记录上 (QComboBox)
- 麻烦得到提升::记录器编译
- C++ 中混合二进制/文本日志记录的最佳做法
- 修改链表中的记录C++
- 使用流在C++中记录信息