【发布时间】:2021-12-13 09:50:41
【问题描述】:
我尝试使用以下代码将对象写入 dat 文件:
#include<iostream>
#include<fstream>
#include<string>
#include<string.h>
using namespace std;
class Student
{ //data members
int adm;
string name;
public:
Student()
{
adm = 0;
name = "";
}
Student(int a,string n)
{
adm = a;
name = n;
}
Student setData(Student st) //member function
{
cout << "\nEnter admission no. ";
cin >> adm;
cout << "Enter name of student ";
cin.ignore();
getline(cin,name);
st = Student(adm,name);
return st;
}
void showData()
{
cout << "\nAdmission no. : " << adm;
cout << "\nStudent Name : " << name;
}
int retAdmno()
{
return adm;
}
};
/*
* function to write in a binary file.
*/
void demo()
{
ofstream f;
f.open("student.dat",ios::binary);
for(int i = 0;i<4;i++)
{
Student st;
st = st.setData(st);
f.write((char*)&st,sizeof(st));
}
f.close();
ifstream fin;
fin.open("student.dat",ios::binary);
Student st;
while(!fin.eof())
{
fin.read((char*)&st,sizeof(st));
st.showData();
}
}
int main()
{
demo();
return 0;
}
但是当我执行演示函数时,我从“student.dat”中得到了一些垃圾值 文件。我正在创建一个数据库并想要获取记录,但我无法获取 dat 文件中的所有记录。
请提出解决方案
【问题讨论】:
-
Student Student::setData(Student st)是一个非常奇怪的成员函数。它是一个成员函数,因此您需要在现有的Student对象上调用它。您需要传递第二个Student对象作为参数st,只是为了让它被忽略和覆盖。最后返回第三个 Student 对象。 -
这是一个常见问题解答。您不能使用 fread/fwrite 或
fstream::{read,write}将std::strings 转储到磁盘。发明一种适当的序列化格式或使用现有的。 Using fread/fwrite for STL string. Is it correct? -
你不能像
std::string这样存储非平凡的对象。阅读有关序列化的信息,或定义更合适的类型。 -
建议:如果您的实际目标是存储学生数据,请尝试提供 JSON 或某种数据库或某种其他文本格式。
标签: c++ windows c++17 file-handling