输出fscanf()不正确

输出fscanf()不正确,c,C,您好,我正在尝试读取一个包含我编写的代码的文件 #include <stdio.h> int main(){ int task_id=0; FILE *fp; fp = fopen("output","r"); if (fp == NULL) { printf("failed opening file"); return 1; } else{ fscanf(fp,"conhost.

您好,我正在尝试读取一个包含我编写的代码的文件

#include <stdio.h>

int main(){
    int task_id=0;
    FILE *fp;
    fp = fopen("output","r");
    if (fp == NULL)
    {   
        printf("failed opening file");
        return 1;
    }
    else{
    fscanf(fp,"conhost.exe                   %d",&task_id);
    fclose(fp);
    printf("taskID is: %d",task_id);
    return 0;
    }
}

我一直以0的形式获取输出,假设您想从示例
输出
文件中获取“2”,您可以这样做:

#include <stdio.h>

int main()
{
    int task_id = 0;
    FILE *fp;
    fp = fopen("output", "r");
    if (fp == NULL) {   
        printf("failed opening file\n");
        return 1;
    }
    else {
        fscanf(fp, "%*s%*d%*s%d", &task_id);
        printf("taskID is: %d\n", task_id);
    }   // Code
    fclose(fp);
    return 0;
}
#包括
int main()
{
int task_id=0;
文件*fp;
fp=fopen(“输出”、“r”);
如果(fp==NULL){
printf(“打开文件失败\n”);
返回1;
}
否则{
fscanf(fp,%*s%*d%*s%d“,&task_id);
printf(“任务id为:%d\n”,任务id);
}//代码
fclose(fp);
返回0;
}

注意
fscanf()
允许您在格式说明符和百分比符号之间添加星号来丢弃数据,因此您可以修改此语句
fscanf(fp,“%$s%*d%*s%d”、&task_2;id)以收集所需数据。

卡拉先生的回答很好(我推荐他使用赋值抑制运算符),但我还要补充一条建议。在任何时候阅读输入行时,请使用面向行的输入函数,如
fgets
,这将帮助您避免使用
scanf
系列函数时遇到的大量陷阱。然后,您可以使用
sscanf
从保存数据行的缓冲区解析所需信息。这确保了输入流中剩余的内容不依赖于所使用的格式说明符

另外,不要硬编码文件名——这就是程序参数的用途。一个简单的例子是:

#include <stdio.h>

#define MAXC 1024u  /* if you need a constant, #define one (or more) */

int main (int argc, char **argv) {  /* don't hardcode name, use arguments */

    int task_id = 0;
    char buf[MAXC];
    /* use filename provided as 1st argument (stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    if (!fgets (buf, MAXC, fp)) {   /* read with line-oriented function */
        fputs ("error: EOF encountered on file read.\n", stderr);
        return 1;
    }
    if (fp != stdin) fclose (fp);   /* close file if not stdin */

    /* parse information with sscanf (read/discard initial string) */
    if (sscanf (buf, "%*s %d", &task_id) != 1) {
        fputs ("error: invalid file format.\n", stderr);
        return 1;
    }
    printf("taskID is: %d\n",task_id);  /* output task_id */
}
#包括
#定义MAXC 1024u/*如果需要常数,请定义一个(或多个)*/
int main(int argc,char**argv){/*不要硬编码名称,使用参数*/
int task_id=0;
char-buf[MAXC];
/*使用作为第一个参数提供的文件名(默认为stdin)*/
文件*fp=argc>1?fopen(argv[1],“r”):stdin;
如果(!fp){/*验证文件是否打开以进行读取*/
perror(“文件打开失败”);
返回1;
}
如果(!fgets(buf,MAXC,fp)){/*使用面向行的函数读取*/
fputs(“错误:读取文件时遇到EOF。\n”,stderr);
返回1;
}
如果(fp!=stdin)fclose(fp);/*如果不是stdin,则关闭文件*/
/*使用sscanf解析信息(读取/放弃初始字符串)*/
如果(sscanf(buf,%*s%d,&任务id)!=1){
fputs(“错误:无效的文件格式。\n”,stderr);
返回1;
}
printf(“任务id为:%d\n”,任务id);/*输出任务id*/
}
示例使用/输出

$ ./bin/rd_task_id <output
taskID is: 4272

$./bin/rd\u task\u id
fscanf
返回什么?如果从
fscanf
检查返回值,会发生什么情况?我打赌它返回0,您还没有读取任何值。您确定您的
输出
文件确实包含
conhost.exe…空格。。。NUMBER
?@TimRandall我认为格式字符串中的一个空格应该会消耗掉所有字符。无法复制,但跳过第一部分的更好方法是使用
int res=fscanf(fp,“%*s%d”,&task\u id)
它扫描字符串直到第一个空格,但不存储它,然后读取
int
。一个更完整的答案-向上投票
$ ./bin/rd_task_id <output
taskID is: 4272