【发布时间】:2016-09-01 17:49:31
【问题描述】:
我正在尝试写N个struct类型的固定长度记录
const int N = 101;
char dummay[] = "#####";
struct hashTable
{
char name[51];
int RRN;
};
int main(){
fstream f("hashFile.txt");
f.seekp(0,ios::beg);
hashTable h;
for (int i = 0 ; i < N ; i++ ) {
strcpy(h.name,dummay);
h.RRN = i;
f.write((char*)&h,sizeof h);
}
当我再次尝试输出这些记录时,只有前 25 条记录运行良好!
f.seekp(0,ios::beg);
for (int i = 0 ; i < N ; i++ )
{
f.read((char*)&h,sizeof h);
cout << h.RRN << endl << h.name << endl;
}
完整的code,为什么会发生这种情况以及如何解决?!
【问题讨论】:
-
使用正确的变量名
-
您在ideone上显示的代码正确输出超过25条记录。
-
我的电脑上只有 25 条记录正确,但是问题解决了,看第一个答案。
标签: c++ file fstream fixed-length-record