c - 通过 C 中的 fgets 解析 .txt 文件的参数

标签 c parsing file-io

我有 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/

相关文章:

database - 在 SQL 中以精美打印的格式选择数组

c - 从 C 文件中读入

c - 为什么链接器不抛出多个函数声明错误?

c - 不同类型如何存储在内存中

c# - 如何解析 C# 代码以找到类的派生类和接口(interface)

java - 从文件创建二叉树

c - 如何使用Arduino的串口打印

java - 解析包含引号和换行符的逗号分隔值

java - 如何确定在 Java 中创建文件时允许的最大路径长度

MySQL如何不断创建唯一文件输出?