| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:文件问题
只看楼主 加入收藏
lovepiss
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-2-24
结帖率:0
收藏
 问题点数:0 回复次数:1 
文件问题
有两个文件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
newyj
Rank: 2
等 级:新手上路
威 望:3
帖 子:542
专家分:0
注 册:2008-1-4
收藏
得分:0 
这个用 批处理 更容易些
但是 效率就不行了
2008-03-17 21:23
快速回复:文件问题
数据加载中...
 
   



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

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