我仍然是C编程的新手,并且在概念方面遇到了一些困难。当我尝试编译程序时,我收到此警告:
1)警告:从不兼容的指针类型传递copyInfo
的参数1
2)警告:从不兼容的指针类型
copyInfo
的参数2
为什么我会收到这些警告?该计划的目标是将所有信息从inputFile
复制到outputFile
。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int badProgram(const char *const program){
printf("The program is missing some of the files!");
return -1;
}
int missingInputTxt(const char *const fileName){
printf("The file is missing! Please provide the file!");
return -1;
}
int copyInfo(FILE *input, FILE *ouput){
char singleLine[150];
int result = 0;
while(fgets(singleLine, sizeof(singleLine), input) != NULL){
fprintf(ouput, "%s\n", singleLine);
}
return 1;
}
int main(int argc, char * argv[])
{
int result = 0;
if(argc < 2){
return badProgram(argv[0]);
}
else{
FILE *input;
FILE *output;
if((input = fopen(argv[1], "r")) == NULL){
return missingInputTxt(argv[1]);
}
if((output = fopen(argv[2], "w")) != NULL){
result = copyInfo(argv[1], argv[2]);
fclose(output);
}
}
return result;
}
答案 0 :(得分:1)
copyInfo
期望FILE*
类型的参数。您使用argv[1]
和argv[2]
来调用它,其类型为char*
。那些是不兼容的类型。
您需要使用:
result = copyInfo(input, output);
答案 1 :(得分:0)
argv[1]
argc[2]
和result = copyInfo(input,ouput); // as input and output are file pointers
不是文件指针。您将错误的类型参数传递给此函数。
像这样写 -
fclose(input);
fclose(output);
此外,您应该关闭您打开的文件。
{{1}}