C++文件的读写
之前定义了一个结构体数组,有元素name,number,room,下面是把数据保存到文件的算法,编译可以通过,也输出了“数据保存成功”,路径中文件也建立了,可是文件中却什么也没有?
……
……
void SqList::Save(fstream& ofile)
{
ofile.clear();
int o=0;
while(o>=length)
{
ofile
<<setw(10)<<left<<element[o].name
<<setw(10)<<left<<element[o].number
<<setw(10)<<left<<element[o].room<<endl;
o++;
}
cout<<"数据文件保存成功!"<<endl;
}
……
……
void main()
{
SqList SL(0);
fstream iofile;
iofile.open("d:\\iofile.txt",ios_base::in|ios_base::out|ios_base::app);
if(!iofile)
cout<<"打开文件失败!"<<endl;
SL.Input();
SL.Save(iofile);
}
[解决办法]
int o=0;
while(o>=length)
确信这个循环会运行?单步跟踪一下
[解决办法]
你有没有关闭文件流或刷新缓冲区这个很有必要