簡體   English   中英

用C中的fgets解析.txt文件的參數

[英]parsing Argument of a .txt file by fgets in C

我有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()要求第一個參數為char數組,第二個參數為第一個參數。

int str[100];
fgets(str, 2, myfile);

應該,

char str[100];
fgets(str, sizeof(str), myfile);

將字符數組轉換為int或double要求另一個函數調用,如atoi()或atof()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM