读书人

二进制读写的有关问题

发布时间: 2013-08-13 16:43:28 作者: rapoo

二进制读写的问题
调试时,到的这一条语句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;
}


[解决办法]


VS提示:First-chance exception at 0x6774CCD2 (msvcp110d.dll) in Hello.exe: 0xC0000005: Access violation writing location 0x00000001.
调试停止在(*_Pnext)->_Myproxy = 0;

问题:是文件指针超出了范围吗?该怎么解决?(50分)

inline void _Container_base12::_Orphan_all()
{// orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{// proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);

for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
(*_Pnext)->_Myproxy = 0;
_Myproxy->_Myfirstiter = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}



源代码:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class student
{
private:
int age; //年龄
string name; //姓名
public:
student(){}
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;
return os;
}
};

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<2;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;
}


错误太多,不一一说了,写了个例子,希望对你有帮助。重点就是 std::string 不能直接复制内存,需要将字符串的大小和内容分别通过二进制格式输出。
ps. 如果你的编译器不认 {...} 的语法,自己改成相应的构造函数即可。

#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;
}

读书人网 >C++

热点推荐