标题:文件问题
取消只看楼主
lovepiss
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-2-24
结帖率:0
 问题点数:0 回复次数:0 
文件问题
有两个文件text1.txt和text2.txt。两个文件中所存内容都是人名,并且每个名字都是一行一行存放。要求编写程序,把两个文件中名字相同的名字存到text3文件中。

这是我写的程序,但结果不正确,请帮忙修改写,谢谢。



#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char str1[20],str2[20];
    ifstream file1,file2;
    ofstream file3;
    file1.open("text1.txt",ios_base::in|ios::binary);
    if(!file1)
    {
        cout<<"text1.txt cannot open"<<endl;
        return 0;
    }
    file2.open("text2.txt",ios::in|ios::binary);
    if(!file2)
    {
        cout<<"text2.txt cannot open"<<endl;
        return 0;
    }
    file3.open("text3.txt",ios::out|ios::binary|ios::app);
    if(!file3)
    {
        cout<<"text3.txt cannot open"<<endl;
        return 0;
    }
    while(!file1.eof())
    {
        file1.getline(str1,20);
        while(!file2.eof())
        {
            file2.getline(str2,20);
            if(strcmp(str1,str2)==0)
                file3.write(str1,sizeof(str1));//这个也有问题,把一写莫名其妙的的汉字也复制上去了,不知道怎么改
        }
               
        file2.seekg(0,ios::beg);//为什么指针不能回到文件开头?
    }
    file1.close();
    file2.close();
    file3.close();

    return 0;
}
搜索更多相关主题的帖子: 文件 
2008-03-17 19:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-203886-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.154951 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved