二进制读写的问题
调试时,到的这一条语句st.update(age,name);出现错误,恳请各位大侠指出错误。
原意是想更新第一条记录的,可是老出现错误。
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
class student
{
private:
int age; //年龄
string name; //姓名
public:
student()=default;
student(int a,string n):age(a),name(n){}
void update(int a,string n) //更新学生数据
{
age=a;
name=n;
}
friend ostream& operator<<(ostream &os,student stu) //重载==操作符
{
os<<"age : "<<stu.age<<ends<<"name : "<<stu.name<<endl;
}
};
void print(fstream &f) //打印所有数据
{
f.seekg(0,f.end);
int n=f.tellg()/sizeof(student);
f.seekg(0,f.beg);
student st;
for(int i=0;i<n;i++)
{
f.read((char*)&st,sizeof(student));
cout<<st;
}
}
int main(int argc, char** argv)
{
fstream f("test.dat",ios::in|ios::out|ios::trunc|ios::binary);
if(!f)
{
cerr<<"file can't open!"<<endl;
exit(-1);
}
student st(18,"nobody");
for(int i=0;i<5;i++) //初始化5条记录
{
f.write((char*)&st,sizeof(student));
}
print(f);
int age;string name;
cout<<"input age,name to update"<<endl;
cin>>age>>name;
st.update(age,name);
f.seekg(0*sizeof(student),f.beg);
f.write((char*)&st,sizeof(student));
print(f);
return 0;
} 二进制文件读写 C++
分享到:
[解决办法]
string是对象,里面包含一些指针,不能直接读写。指针是内存地址,直接写地址到文件,下次再读出来的地址是没有意义的。
string按二进制读写,要读写string实际的内容,而不是读写string本身。
写:先写一个string.size(),然后把string.data()用二进制方式写进去。
读:先读一个长度(4字节),根据这个长度,决定后面应该读几个字节的内容,再生成一个string。
[解决办法]
#include<iostream>
#include<fstream>
//#include<cstdlib>
#include <string>
#include <cstring>
using namespace std;
class student
{
private:
int age; //年龄
//string name; //姓名
char name[64];
public:
student(){}
student(int a,string n):age(a)
{
strncpy_s(name, n.c_str(), n.length());
}
void update(int a,string n) //更新学生数据
{
age=a;
strncpy_s(name, n.c_str(), n.length());
}
friend ostream& operator<<(ostream &os,student stu) //重载==操作符
{
return os<<"age : "<<stu.age<<ends<<"name : "<<stu.name<<endl;
}
};
void print(fstream &f) //打印所有数据
{
f.seekg(0,f.end);
int n=f.tellg()/sizeof(student);
f.seekg(0,f.beg);
student st;
for(int i=0;i<n;i++)
{
f.read((char*)&st,sizeof(student));
cout<<st;
}
}
int main(int argc, char** argv)
{
fstream f("test.dat",ios::in
[解决办法]
ios::out
[解决办法]
ios::trunc
[解决办法]
ios::binary);
if(!f)
{
cerr<<"file can't open!"<<endl;
exit(-1);
}
student st(18,"nobody");
for(int i=0;i<5;i++) //初始化5条记录
{
f.write((char*)&st,sizeof(student));
}
print(f);
int age;string name;
cout<<"input age,name to update"<<endl;
cin>>age>>name;
st.update(age,name);
f.seekg(0*sizeof(student),f.beg);
f.write((char*)&st,sizeof(student));
print(f);
return 0;
}
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct student
{
int age;
string name;
student(){}
student(int a,string const& n):age(a),name(n){}
friend ostream& operator<<(ostream &os,student const& stu)
{
os<<"age : "<<stu.age<<ends<<"name : "<<stu.name;
return os;
}
};
// read a binary file as a sequence of student object.
// assuming no memory gaps between objects.
void read (fstream& f, std::vector<student>& students)
{
while (f)
{
students.push_back({});
student& s = students.back();
f.read(reinterpret_cast<char*>(&s.age),sizeof(s.age));
int size = 0;
f.read(reinterpret_cast<char*>(&size),sizeof(size));
s.name.resize(size);
f.read(&s.name[0],size);
}
}
// write a sequence of student objects in binary format.
// all memory laid out immediately without gaps between objects.
void write (fstream& f, std::vector<student>const& students)
{
for (size_t i = 0, I = students.size(); i != I; ++i)
{
student const& s = students[i];
f.write(reinterpret_cast<char const*>(&s.age),sizeof(s.age));
int const size = s.name.size();
f.write(reinterpret_cast<char const*>(&size),sizeof(size));
f.write(&s.name[0],size);
}
}
int main ()
{
fstream f("test.dat",ios::in
[解决办法]
ios::out
[解决办法]
ios::trunc
[解决办法]
ios::binary);
if(!f)
{
cerr<<"file can't open!"<<endl;
exit(-1);
}
std::vector<student> students;
students.push_back({18,"nobody"});
students.push_back({19,"somebody"});
std::cout << students[0] << std::endl;
std::cout << students[1] << std::endl;
// write to file in binary format.
write(f,students);
// delete all students.
students.clear();
// read back students previously stored in file.
f.seekg(0,ios::beg);
std::cout << "read binary" << std::endl;
read(f,students);
std::cout << students[0] << std::endl;
std::cout << students[1] << std::endl;
}