方法和上面的差不多。
代码如下:
#include<iostream>
#include<fstream.h>
#include<string>
//using std::cout;
//using std::cin;
using std::string;
int main()
{
ifstream ifs("file1.text");
char p[100]={0};
char q[100]={0};
string str1,str2;
fstream ofs("file2.text",ios::in|ios::out|ios::app);
for(int i=0;!ifs.eof();i++)
{
ifs.getline(p,100);
str1=p;
int m=0; //定义m,如果m为1,则file1遍历file2时有相同的
for(int j=0;!ofs.eof();j++)
{
ofs.getline(q,100);
str2=q;
if(str1==str2) //
m=1;
}
ofs.clear(); //清除eof标志位
if(0==m) //m为0时向文件file2输入
ofs<<'\n'<<p;
ofs.seekp(0,ios::beg);
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考