gpt4 book ai didi

c - C 练习文件中的 Fscanf

转载 作者:行者123 更新时间:2023-11-30 19:42:52 24 4
gpt4 key购买 nike

我正在学习 C 语言编程考试,我正在思考和解决练习。具体来说,我在 fscanf 上跳了一个错误(终端:“从不兼容的指针类型传递'fscanf'的参数1 [默认启用]...”)并且不知道如何修复它。谢谢问候

“给定文件 data.txt 整数 1 行的程序,读取全部内容并可视化最小奇数,函数应该执行此操作并返回最小值,即屏幕上显示的主程序”

 #include <stdio.h>

FILE* fichero;

int little(){
//open the file
char name[10] = "datos.txt";
fichero = fopen( name,"r"); //opened in read mode
int whatis,minimpar=99999999;
while(!EOF){
fscanf("%d",&whatis); //We took the number to the variable until we reach the end of file
if (whatis%2==1){ //If the variable is odd
if(whatis<minimpar){ //And is smaller than we have saved
minimpar=queserasera;
}
}
}
fclose(fichero);
return minimpar; //Return variable
}

int main(void){
int a;
a=little();
printf("The smallest of the odd file is: %d",a);
return 0;
}

最佳答案

这个while(!EOF)总是错误的,试试while(fscanf(fichero, "%d", &whatis) == 1),原因是EOF 是 I/O 函数返回的常量值,表示当前 I/O 操作失败,因为文件位于末尾。

此外,fscanf() 将您要扫描的文件作为第一个参数,您的方式会导致未定义的行为,因为字符串文字将被视为 文件*.

您必须专门启用编译器警告,因为您正在学习,在我看来,编译器警告和错误消息是一个很好的学习来源。

关于c - C 练习文件中的 Fscanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30425604/

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