【发布时间】:2013-12-18 18:03:41
【问题描述】:
我有 2 个 txt 文件,其中的数字 > 0,我必须对它们进行组合和排序。也不能有 2 个相同的值。
这里是文件的值。 文件1:
1
2
3
4
5
6
7
文件2:
1
3
6
8
10
输出应如下所示:
1
2
3
4
5
6
7
8
10
我目前的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fr1,*fr2;
int fst, snd, p[256], i=0, n=0;
bool f1=true,f2=true;
fr1 = fopen("txt/cisla.txt","r");
fr2 = fopen("txt/cisla2.txt","r");
while(feof(fr1) == 0 && feof(fr2) == 0)
{
if (f1) fscanf(fr1, "%d", &fst);
if (f2) fscanf(fr2, "%d", &snd);
printf("%d - %d\n", fst, snd);
if (fst == snd)
{
f1 = true;
f2 = true;
p[i] = fst;
} else if (fst > snd)
{
p[i] = snd;
f1 = false;
f2 = true;
} else
{
f2 = false;
f1 = true;
p[i] = fst;
}
i++;
}
fclose(fr1);
fclose(fr2);
printf("\n\n\n");
for(int j = 0; j < i; j++)
{
printf("%d\n", p[j]);
}
return 0;
}
这样的结果是:
1 - 1
2 - 3
3 - 3
4 - 6
5 - 6
6 - 6
7 - 8
1
2
3
4
5
6
7
底部是数组。在顶部,我正在编写读取的值。问题是它似乎停在第一个文件的末尾,但我希望它继续第二个文件,即使第一个文件在末尾p>
【问题讨论】:
-
您的文件已排序,对吧?
-
system("cat file1 file2 > sort");?
标签: c++