select new
{
FileName = img
}
一句话取指定目录下面的指定文件,然后Linq到一个匿名对象了。你如果取得要存其他内容,就加属性。然后就调你写的存数据的方法了
你这么做肯定是有问题的,sizeof(pe) 或sizeof(People) 获取的并不是写入实际数据的长度, 而只是People的长度, 这个长度是固定的。正确的做法可以通过两种方式来处理:
1、 通过重载流输出/输入流
ostream&operator <<(ostream&os, People&p) {
// people 转换成公有成员, 或在类中friend 该函数
os <<p.people.name <<p.people.weight // .... 按需输出成员
return os
}
读取通过重载流输入
istream&operator >>(istream&in, People&p) {
in >>p.people.name >>p.people.weight // .... 按照写入成员顺序将所有成员读取完成
return in
}
main() 中的file.write() 改成 file <<pe
main() 中的file.read() 改成 file >>pe2
2、将people 结构中的成员都换成定长数组。
如:
struct
{
char name[32]
char weight[32]
....
}people
main() 函数的file.write 改成:file.write(&pe.people, sizeof(pe.people))
main() 函数的file.read 改成:file.read(&pe.people, sizeof(pe.people))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)