我有2個txt文件,其中數字> 0,我必須對它們進行組合和排序。也不能有2個相同的值。合併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
底部是數組。在頂部,我正在寫入所讀取的值。事情似乎停止在第一個文件的結尾,但我希望它繼續第二個,即使第一個文件在結尾處
您的文件進行排序,對吧? – dasblinkenlight
是的,他們排序 – DeiForm
'系統(「貓文件1文件2>排序」);'? –