简体   繁体   English

通过fscanf()读取的值未打印出来

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

I am trying to print out the first 3 line from a text file. 我正在尝试从文本文件中打印出前3行。

states 10
start  0
accept 9

Code: 码:

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);
}

The output I am receiving: 我收到的输出:

states: 32528
start: -29575952
accpet: 32767

After printing out the what's in the file. 打印出文件中的内容之后。 and prints out the random number, I cannot seem to get the states, start, and accept number. 并打印出随机数,我似乎无法获取状态,开始并接受数字。

Thanks. 谢谢。

It seems you don't use fscanf properly. 看来您没有正确使用fscanf。 The first argument should be file descriptor: 第一个参数应该是文件描述符:

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

you can see usage (for example) at: http://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm 您可以在以下位置查看用法(例如): 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