33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<fstream>
//#include<cstdlib>
#include <string>
#include <cstring>
........
........
}
[/quote]
#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;
}
#include<iostream>
#include<fstream>
//#include<cstdlib>
#include <string>
#include <cstring>
........
........
}
[/quote]
用VS2012重新调试了一下,原来问题不是出现在st.update(age,name);
而是在运行完print(f);后
运行结果:
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;
}
[/quote]
错误太多,不一一说了,写了个例子,希望对你有帮助。重点就是 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;
}
#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;
}
class student
{
private:
int age; //年龄
string name; //姓名
//..
f.read((char*)&st,sizeof(student));
f.write((char*)&st,sizeof(student));
你可以知道name是较复杂的c++对象。
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<<endl<<"name : "<<stu.name<<endl;
}
};
int main(void)
{
student st(18,"nobody");
int age;
string name;
cout<<"input age,name to update"<<endl;
cin>>age>>name;
st.update(age,name);
cout << st << endl;
}
结果:
input age,name to update
100
csdn
age : 100
name : csdn