首页 > 解决方案 > 从文件中的特定位置读取特定数据(C)

问题描述

如果我有这个包含 Integers 的 .txt 文件:

 - //firstRow 14 5
 - //secondRow 5
 - //fourthRow 3
 - //fourthRow 3

如何从第一行读取第二个 整数?谢谢

标签: cfile

解决方案


fscanf(fptr, "[^\n]", file);

scanf 函数在第一次遇到时很难理解。您的格式字符串(第二个参数)是错误的。你想要更多类似的东西

int a, b, n;
while( (n = fscanf(fptr, "%d%d", &a, &b)) != EOF ) {
    if( n == 2 ) { ... your code here ... }
}

准备好花时间阅读手册。 man fscanf是您的朋友,但需要一些时间来热身。


推荐阅读