gpt4 book ai didi

c - 警告从不兼容的指针类型传递参数 1 of ‘fopen’

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:55 24 4
gpt4 key购买 nike

我的程序将两个文件作为参数进行了排序,并将其混合到一个名为 final_sorted.txt 的文件中。程序成功运行并创建了混合文件,甚至忽略了重复的单词,但编译器告诉我一些警告是没有被移除。

equipo01@equipo01-desktop:~/Escritorio/mezclar (2)$ gcc meclapro.c -o mixmeclapro.c: In function ‘mix_files’:
meclapro.c:10: warning: passing argument 1 of ‘fopen’ from incompatible pointer type
/usr/include/stdio.h:249: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
meclapro.c:11: warning: passing argument 1 of ‘fopen’ from incompatible pointer type
/usr/include/stdio.h:249: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
meclapro.c: In function ‘main’:
meclapro.c:69: warning: passing argument 1 of ‘mix_files’ from incompatible pointer type
meclapro.c:6: note: expected ‘char **’ but argument is of type ‘char *’
meclapro.c:69: warning: passing argument 2 of ‘mix_files’ from incompatible pointer type
meclapro.c:6: note: expected ‘char **’ but argument is of type ‘char *’

这是我的代码,它带哪个带命令行参数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void mix_files(char **file1, char **file2){

FILE *a1, *a2, *output;
char aux1 [10000],aux2 [10000];
a1 = fopen(file1, "r");
a2 = fopen(file2, "r");
ouput = fopen ("final.txt", "w+");

// read the first line of each file:
fscanf(a1,"%s",aux1);
fscanf(a2,"%s",aux2);
// loop, while !feof for both file
while(!feof(a1) && !feof(a2)) {
// Select the line to add
if(strcasecmp(aux1,aux2) < 0){
// add the line
fprintf(output,"%s\n",aux2);
//read the next line from aux2
fscanf(a2,"%s",aux2);
}

else if(strcasecmp(aux1,aux2)>0){
fprintf(salida,"%s\n",aux1);
fscanf(a1,"%s",aux1);
}

if (strcasecmp(aux1,aux2)==0){
//printf("repetidas\n");
fprintf(salida,"%s\n",aux1);
fscanf(a1,"%s",aux1);
fscanf(a2,"%s",aux2);
}
}

if(!feof(a1)){
while(!feof(a1)) {
fscanf(a1,"%s",aux1);
fprintf(salida,"%s\n",aux1);
}
}
if(!feof(a2)){
while(!feof(a2)) {
fscanf(a2,"%s",aux2);
fprintf(salida,"%s\n",aux2);
}
}

}

int main(int argc, char *argv[]){

mix_files(argv[2], argv[1]);

return(0);
}

我希望有人帮我找出警告的原因以及我该如何解决,在此先感谢您的回答,对不起我的英语

最佳答案

改变:

void mix_files(char **file1, char **file2){

到:

void mix_files(char *file1, char *file2){

或者更好:

void mix_files(const char *file1, const char *file2){

然后您会将正确的类型 (char *) 从 main 传递到 mix_files,这反过来也会解决您调用 fopen 时的问题。

关于c - 警告从不兼容的指针类型传递参数 1 of ‘fopen’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4085773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com