我有 Members.txt
文件,我正在阅读以下项目:
3
Rebaz salimi 3840221821 09188888888
4
95120486525
下面的代码给了我这个运行时错误:
warning: passing argument 1 of 'fgets' from incompatible pointer type|
C语言代码:
FILE *fp,*ft,*fs;
struct MEMBERS
{
int code;
char Fname[40];
char Lname[40];
int Pnum[20];
float bedeh;
float credit;
int count[1];
struct meroDate issued;
struct meroDate duedate;
};
struct MEMBERS b;
int main()
{
int i = 0, line = 5;
char w[100];
int str[100];
FILE *myfile;
myfile = fopen("Members.txt","r");
if (myfile== NULL)
{
printf("can not open file \n");
return 1;
}
while(line--){
fgets(str,2,myfile);
if(sscanf(str, "%d", &b.count[0])==1)){
printf("%d\n", b.count[0]);
i++;
}
}
fclose(myfile);
return 0;
}
知道问题出在哪里吗?
最佳答案
使用时请引用C函数的声明或手册页。 fgets() 需要第一个参数的字符数组, 和第二个参数的第一个参数的大小。
int str[100];
fgets(str, 2, myfile);
应该是,
char str[100];
fgets(str, sizeof(str), myfile);
将字符数组转换为 int 或 double 需要另一个函数调用,如 atoi() 或 atof()。
关于c - 通过 C 中的 fgets 解析 .txt 文件的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733248/