关于文件流的文件指针问题(在线)
在程序里我做一个循环,往dat文件里输入多条记录,查看时只有一条记录,是什么原因?是不是写入时的文件指针指向有问题(是不是该把指针放在文件尾,该怎么放?),以下是代码:
for(i=1;i<=3;i++)
{
ofstream output_file;
output_file.open(result_file);
if(!output_file)
cout<<"Can't open the file!"<<endl;
output_file<<"输入日期-1:"<<y<<"-"<<m<<"-"<<d<<" 星期"<<now.CountWeekday()<<endl;
output_file<<"今日安排:面向对象程序还未调试完毕,继续."<<endl;
output_file.close();
}
问题点数:20、回复次数:4Top
1 楼UPCC(杂食动物)回复于 2004-08-01 10:23:28 得分 8
output_file.open("D:\\test.txt",ios_base::out | ios_base::app );
你的问题是文件在打开是要用“添加”的方式打开,不然的循环一次就打开一次,而就更新一次的内容,这样你保存的就只能是一次的而已Top
2 楼lovecrayfish(阿龙)回复于 2004-08-01 11:41:17 得分 0
好像应该是output_file.open("D:\\test.txt",ios::out | ios::app );可是我试了,也不行,就是在一次循环里也是如此,后面写的会覆盖前面的内容
Top
3 楼erwinrommel(平沙落雁)(灌水是程序员的美德)回复于 2004-08-01 12:11:01 得分 8
如果你的程序希望是每次运行重新记录的话,那么这样
搂主,把你的
ofstream output_file;
output_file.open(result_file);
for(i=1;i<=3;i++)
{
if(!output_file)
cout<<"Can't open the file!"<<endl;
output_file<<"输入日期-1:"<<y<<"-"<<m<<"-"<<d<<" 星期"<<now.CountWeekday()<<endl;
output_file<<"今日安排:面向对象程序还未调试完毕,继续."<<endl;
}
output_file.close();
Top
4 楼erwinrommel(平沙落雁)(灌水是程序员的美德)回复于 2004-08-01 12:12:23 得分 4
如果你希望每次运行时是追加内容的话,用一楼和二楼的方法!Top




