文件中指针移动的问题
这程序的目的是将a.txt和b.txt作比较,把b.txt中有的而a.txt中没有的,输出到c.txt中。
源程序如下:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
void main()
{
ifstream fa;//fa指向a.txt
fa.open("a.txt",ios::in);
if(!fa)
{
cout<<"不能打开 a.txt!"<<'\n';
exit(1);
}
ifstream fb;//fb指向b.txt
fb.open("b.txt",ios::in);
if(!fb)
{
cout<<"不能打开 b.txt!"<<'\n';
exit(1);
}
ofstream fc;//fc指向c.txt
fc.open("c.txt",ios::app);
if(!fc)
{
cout<<"不能打开 c.txt!"<<'\n';
exit(1);
}
/*-------------操作--------------------------*/
char buff_b[60],buff_a[60];
int tag=0;
while(fb.getline(buff_b,60))
{
cout<<buff_b<<'\n';
while(fa.getline(buff_a,60))
{
cout<<" "<<buff_a<<'\n';
if(strcmp(buff_a,buff_b)==0)
{
tag=1;
cout<<" break1"<<'\n';
break;
}
}
{
if(fa.seekg(0))//将fa中的指针移到开头
cout<<"-----------移动指针-----------\n";
}
if(tag==0)
{
fc<<buff_b<<'\n';
}
else
{
tag=0;
}
}
/*-----------------------------------------------*/
fa.close();
fb.close();
fc.close();
}
a.txt为:
强大
深蓝色
备选
工人
有限责任公司
工厂
转会
b.txt为:
强大
工人
工作
工厂
你好
显示器上输出如下:
强大
强大
break1
-----------移动指针-----------
工人
强大
深蓝色
备选
工人
break1
-----------移动指针-----------
工作
强大
深蓝色
备选
工人
有限责任公司
工厂
转会
工厂
你好
其中出现了一个奇怪的问题:在“工作”比较完之后fa.seekg(0)不在起作用,不能将指针回到开头。
请问 这是什么原因?该怎么办?