更具体地说,我想将两个排序的整数列表合并到也进行排序的第三个列表中。我不知道为什么我的代码无法正常工作!这是一些条目和我编写的代码的示例:
文件1:
1
2
3
4
5
6
文件2:
7
8
9
9
9
10
C代码:
#include <stdio.h>
#include <stdlib.h>
void merge(char* arq1, char* arq2){
FILE* final;
final = fopen("merge.txt",'w');
FILE* f1 = fopen(arq1,'rt');
FILE* f2 = fopen(arq2,'rt');
if(!f1 || !f2) exit(1);
// n1 and n2 represents the actual value that's been read. r1 and r2 the status of each file.
int n1,n2,r1,r2,equal;
r1 = fscanf(f1,"%d",&n1);
r2 = fscanf(f2,"%d",&n2);
while(r1 || r2){
if((!r1) || (n2 < n1)){
fprintf(final,"%d ",n2);
r2 = fscanf(f2,"%d",&n2);
}else if((!r2) || (n1 < n2)){
fprintf(final,"%d ",n1);
r1 = fscanf(f1,"%d",&n1);
}else{
equal = n1;
fprintf(final,"%d ",equal);
while(n1 == equal) r1 = fscanf(f1,"%d",&n1);
while(n2 == equal) r2 = fscanf(f2,"%d",&n2);
}
}
fclose(final);
fclose(f1);
fclose(f2);
}
int main(int argc, char const *argv[]){
merge("merge1.txt","merge2.txt");
return 0;
}
该算法的工作原理如下:
尽管至少一个文件仍具有内容,但是其内容将随后保存在merge.txt
文件中。如果文件1和2具有相同的内容,我将保存内容并读取2个文件,直到它们找到不同的值,以使合并的文件没有重复的值。
如何将两个已排序的文件合并为一个已排序的文件?
答案 0 :(得分:2)
fopen()
的模式参数应该是字符串。
fscanf()
可以在错误或EOF时返回EOF(-1)
失败时,fscanf()
的目标变量的值未定义。
#include <stdio.h>
#include <stdlib.h>
void merge(char* arq1, char* arq2){
FILE *final;
final = fopen("merge.txt", "w"); // <<--(1)
FILE *f1 = fopen(arq1, "rt"); // <<--(1)
FILE *f2 = fopen(arq2, "rt"); // <<--(1)
if(!f1 || !f2 || !final) exit(1); // <<--(1)
// n1 and n2 represents the actual value that's been read. r1 and r2 the status of each file.
int n1,n2,r1,r2,equal;
r1 = fscanf(f1, "%d", &n1);
r2 = fscanf(f2, "%d", &n2);
while(r1>0 || r2>0){ // <<--(2)
if(r1 < 1 || n2 < n1){ // <<--(2)
fprintf(final, "%d ", n2);
r2 = fscanf(f2, "%d", &n2);
}else if(r2 < 1 || n1 < n2){ // <<--(2)
fprintf(final, "%d ", n1);
r1 = fscanf(f1, "%d", &n1);
}else{
equal = n1;
fprintf(final, "%d ", equal);
while(n1 == equal && r1 > 0) r1 = fscanf(f1, "%d", &n1); // <<--(2,3)
while(n2 == equal && r2 > 0) r2 = fscanf(f2, "%d", &n2); // <<--(2,3)
}
}
fclose(final);
fclose(f1);
fclose(f2);
}
int main(int argc, char const *argv[]){
merge("merge1.txt","merge2.txt");
return 0;
}
答案 1 :(得分:-1)
OP发布的代码无法干净地编译!
这是编译器的输出:
gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c"
untitled.c: In function ‘merge’:
untitled.c:6:31: warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
final = fopen("merge.txt",'w');
^~~
In file included from untitled.c:1:0:
/usr/include/stdio.h:232:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern FILE *fopen (const char *__restrict __filename,
^~~~~
untitled.c:7:27: warning: multi-character character constant [-Wmultichar]
FILE* f1 = fopen(arq1,'rt');
^~~~
untitled.c:7:27: warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
In file included from untitled.c:1:0:
/usr/include/stdio.h:232:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern FILE *fopen (const char *__restrict __filename,
^~~~~
untitled.c:8:27: warning: multi-character character constant [-Wmultichar]
FILE* f2 = fopen(arq2,'rt');
^~~~
untitled.c:8:27: warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
In file included from untitled.c:1:0:
/usr/include/stdio.h:232:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern FILE *fopen (const char *__restrict __filename,
^~~~~
untitled.c: In function ‘main’:
untitled.c:33:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc, char const *argv[]){
^~~~
untitled.c:33:32: warning: unused parameter ‘argv’ [-Wunused-parameter]
int main(int argc, char const *argv[]){
^~~~
关于:
final = fopen("merge.txt",'w');
“ mode”参数必须为字符串。建议在“ mode”参数周围使用双引号:
final = fopen("merge.txt","w");
关于:
FILE* f1 = fopen(arq1,'rt');
和以前一样,“ mode”参数必须为字符串。建议:
FILE* f1 = fopen(arq1,"rt");
对于main()
函数,由于未使用参数,建议使用签名:
int main( void )
编译时,请始终启用警告,然后修复这些警告