工作中急需用到,200分求答案,如果符合要求,立即给分。

lovelife54 2003-12-11 12:03:57
就是找出两个文件中的不同行。
如文件1:
郑州卷烟20031105010000
郑州卷烟20031105010001
郑州卷烟20031105010002
共大约2万行,

文件2和它差不多一样,只是中间可能多或少些行。
怎么用C语言代码(或提供代码思路)找出那些多的和少的行。
要求最好不用数据库。
运行耗时最好在5分钟之内。
答案合理,立即给分,不够再加,你说给多少就给多少。
...全文
35 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
broadoceans 2003-12-11
  • 打赏
  • 举报
回复
可以用文件镜像,速度要块多了。
那么比较时就可以直接读取内存了。
记住,换行的ascii为13,10两个字节
AkiraChing 2003-12-11
  • 打赏
  • 举报
回复
每一行的数据长度都是一样的吗,

我的思路是把每一行的文本都减去第一行,
比如说
郑州卷烟20031105010000
郑州卷烟20031105010001
0 0 0 0 00000000000001


郑州卷烟20031105010000
郑州卷烟20031105010002
0 0 0 0 00000000000002
那么就会得到一组数据
然后根据这组数据就可以得到不同的行了,比如为0的行,为1的行,以此类推,
ISunwu 2003-12-11
  • 打赏
  • 举报
回复
把两个文件装入两个链表。
比较这两个链表,相同的立即删除,应该在1分钟左右能搞定。
我以前做过类似的。
lovelife54 2003-12-11
  • 打赏
  • 举报
回复
这样速度很慢,比较次数太多。
sinhighly 2003-12-11
  • 打赏
  • 举报
回复
fopen
后一行一行读出来比较
strcmp嘛
xiaohedou 2003-12-11
  • 打赏
  • 举报
回复
1)broadoceans(broadoceans) 的说法也很好!快!

直接用内存映射文件访问文件.

首先在C盘下创建一个Mapping.txt里面输入1234567

HANDLE hFile=CreateFile("c:\\mapping.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
//OPEN_EXISTING,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);

HANDLE hFilemap = CreateFileMapping(HFILE,NULL,PAGE_READWRITE,0,1024, NULL);

LPVOID pVoid=MapViewOfFile(hFilemap,FILE_MAP_ALL_ACCESS,0,0,0);

char *Buf=(char *)pVoid;

Buf[0]="1T";
Buf[1]="2";
Buf[2]="3";
Buf[3]="4";

CloseHandle(hFile);
CloseHandle(hFilemap);
UnmapViewOfFile(pVoid);

2)CStdioFile打开文件然后,Open->ReadString->memmory->比较->next->eof()?=Close
lovelife54 2003-12-11
  • 打赏
  • 举报
回复
flying520520(老北风),请提供详细一点的例子好吗?谢谢!在线等。
flying520520 2003-12-11
  • 打赏
  • 举报
回复
还有,有要进得多余的运算,那会很花时间,最好用原始C语言写,绝对会在很小和时间完成任务。
n6002 2003-12-11
  • 打赏
  • 举报
回复
源码早就有
http://www.microsoft.com/downloads/details.aspx?FamilyID=31940f4f-734f-4218-964e-d06c535b4677&DisplayLang=en
察看其中windiff中的COMPITEM.C的源代码。下面是文件的一部分。
/****************************** Module Header *******************************
* Module Name: COMPITEM.C
*
* Module which does the comparison between two files.
*
* Functions:
*
* ci_copytext()
* ci_makecomposite()
* ci_compare()
* ci_onesection()
* compitem_new()
* compitem_delete()
* compitem_discardsections()
* compitem_getcomposite()
* compitem_getleftsections()
* compitem_getrightsections()
* compitem_getleftfile()
* compitem_getrightfile()
* compitem_getstate()
* compitem_gettext_tag()
* compitem_gettext_result()
* compitem_getfilename()
* compitem_frefilename()
*
* Comments:
*
* This module uses the structure compitem which is a data type that knows
* about two files, and can compare them. The result of the comparison
* is a list of sections for each file, and a composite list of sections
* representing the comparison of the two files.
*
* A compitem has a state (one of the integer values defined in state.h)
* representing the result of the comparison. It can also be
* queried for the text result (text equivalent of the state) as well
* as the tag - or title for this compitem (usually a text string containing
* the name(s) of the files being compared).
*
* A compitem will supply a composite section list even if the files are
* the same, or if there is only one file. The composite section list will
* only be built (and the files read in) when the compitem_getcomposite()
* call is made (and not at compitem_new time).
*
*
****************************************************************************/
flying520520 2003-12-11
  • 打赏
  • 举报
回复
这个问题不是很难,但要求速度的话,要注意:访问磁盘的次数不能过多,但一次读入内存中的数据也不要过多,这样会占用很多内存,折中一下。
还有,不要用过于复杂数据结构,用数组最快了,只是注意定位,或者用构体。

老板来了,走了,有空给你代码。
nobounded 2003-12-11
  • 打赏
  • 举报
回复
同意用文件镜象,速度快,如果只是简单的多或少,建议先比较文件大小,再进行实际的比较,详细情况你一想就知道了。
如果只有少数的差别和少量的不变化,简单的比较就可以了,否则,用KMP可以节省大量的时间。
用位比较比字符比较要快,如:01000001和01000002的位比较比比较ASCII的A,B要快,一个&就可以了。
13880079673 2003-12-11
  • 打赏
  • 举报
回复
不行,是C++,C++里面才有容器的概念,如果你有C++的编译器,把以上代码编译一下以可以用,如果没有的话,你把QQ给我,我把生成的exe文件传给你
lovelife54 2003-12-11
  • 打赏
  • 举报
回复
13880079673(CMonkey)朋友,
你的方法看不大懂,能用标准C实现吗?
lovelife54 2003-12-11
  • 打赏
  • 举报
回复
我马上试验,如果行的话,立即给分,如果有好的方法,欢迎继续。
13880079673 2003-12-11
  • 打赏
  • 举报
回复
我给的答案是平时工作总经常用到的东西,对照100w*100w行的数据不超过5分钟
canjian 2003-12-11
  • 打赏
  • 举报
回复
做成两个链表,然后排序,再按 ISunwu(刀口蒙特) 提供的方法来搞定,速度应该还可以
if(list[i] == list[j])// 假设按升序 ,只是描述一下思路
{
remove(list[i]) ;
remove(list[j]) ;
i++ ; //得到下一个节点
j++ ;
}
else if(list[i] > list[j])
{
remove(list[j]) ;
j++ ;
}
else
{
remove(list[i]) ;
i++ ;
}
这样可以将比较次数降到最低
13880079673 2003-12-11
  • 打赏
  • 举报
回复
#pragma warning(disable:4786)

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

void main()
{
ifstream file1("文件1");
ifstream file2("文件2");

ofstream outfile1("d:\\out1.txt");
ofstream outfile2("d:\\out2.txt");

set<string> set1;
set<string> set2;
set<string> set3;
set<string> set4;

string str = "";

while(!file1.eof())
{
file1>>str;
set1.insert(str);
}
file1.close();

while(!file2.eof())
{
file2>>str;
set2.insert(str);
}
file2.close();

cout << "Start!" << endl;

set <string>::iterator iter2;
set <string>::iterator iter;

for(iter2 = set2.begin(); iter2 != set2.end(); ++iter2)
{
iter = set1.find(*iter2);
if (iter == set1.end())
{
set3.insert(*iter2);
}
}
for(iter2 = set1.begin(); iter2 != set1.end(); ++iter2)
{
iter = set2.find(*iter2);
if (iter == set2.end())
{
set4.insert(*iter2);
}
}

for(iter = set3.begin(); iter != set3.end(); ++iter)
{
outfile1 << *iter << endl;
}
outfile1.close();
for(iter = set4.begin(); iter != set4.end(); ++iter)
{
outfile2 << *iter << endl;
}
outfile2.close();

}
用上面的程序就可以:
得到文件1比文件2多的行存入out1
得到文件1比文件2少的行存入out2

wsh_yao 2003-12-11
  • 打赏
  • 举报
回复
不知道你的文件是不是有什么规律啊。具体说说。
也许可以用分配排序(非基于比较)搞定。
matiao 2003-12-11
  • 打赏
  • 举报
回复
如果文件中每一行都有“郑州卷烟”的话,可以用文本编辑程序(如UltraEdit)把它按列选择的方式去掉,节省时间。或者如果只有明显的几次变化的话,拆成几个文件来比较也可以。象时间 2003也可考虑这样处理。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧