簡體   English   中英

通過fscanf()讀取的值未打印出來

[英]Values read via fscanf() not printing out

我正在嘗試從文本文件中打印出前3行。

states 10
start  0
accept 9

碼:

int main(int argc, char*argv[]){
    FILE *fp;
    fp = fopen(argv[1], "r");   
    printf("File is open successfully.\n");
    char *ptr, buf[256];
    int states; //states
    int start; //start
    int accept; //accept

    while((ptr = fgets(buf, 256, fp)) != NULL){
        printf("%s", ptr);
        //process this line from the file
    }
    printf("============================\n");
    char s1[100], s2[100], s3[100];
    fscanf("%s %i",s1, &states);
    fscanf("%s %i",s2, &start);
    fscanf("%s %i",s3, &accept);
    printf("states: %i\n start: %i\n accpet: %i\n", states, start, accept);
}

我收到的輸出:

states: 32528
start: -29575952
accpet: 32767

打印出文件中的內容之后。 並打印出隨機數,我似乎無法獲取狀態,開始並接受數字。

謝謝。

看來您沒有正確使用fscanf。 第一個參數應該是文件描述符:

int fscanf(FILE *stream, const char *format, ...)

您可以在以下位置查看用法(例如): http : //www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm

暫無
暫無

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

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