如何读取在c中新行之后出现的整数?

如何读取在c中新行之后出现的整数?,c,scanf,C,Scanf,我可以使用fscanf()从文件中读取整数。如何读取换行符后面的整数?-请看示例 以下是您似乎想要的: int fscanf(FILE *stream, const char *format, ...); #包括 int main() { int n; 文件*pFile; pFile=fopen(“myfile.txt”、“r”); //根据需要重复多次 fscanf(pFile,\n%d,&n); printf(“%d\n”,n); fclose(pFile); 返回0; } 扫描直到换行,

我可以使用
fscanf()
从文件中读取整数。如何读取换行符后面的整数?

-请看示例

以下是您似乎想要的:

int fscanf(FILE *stream, const char *format, ...);
#包括
int main()
{
int n;
文件*pFile;
pFile=fopen(“myfile.txt”、“r”);
//根据需要重复多次
fscanf(pFile,\n%d,&n);
printf(“%d\n”,n);
fclose(pFile);
返回0;
}

扫描
直到换行,然后读取一个
int
。e、 g.执行:

#include <stdio.h>

int main()
{
    int n;
    FILE * pFile;

    pFile = fopen("myfile.txt", "r");

    // Repeat this as many times as necessary
    fscanf(pFile, "\n%d", &n);
    printf("%d\n", n);

    fclose(pFile);

    return 0;
}

如果行中有更多内容,则可能需要修改格式。

除了他说的是在换行之后,而不是在换行之前。@jet,
fscanf
是否在内部设置文件偏移量,以便下次调用将返回下一个匹配元素?@lexer-Yup。不过,您可以使用
fseek()
将“内部偏移量”移动到不同的位置。如果您想要不同于我下面给出的答案,我们需要更多详细信息。换行符是空白。它被
scanf()
转换说明符
“%d”
(以及其他说明符)忽略。处理换行后的整数不需要任何特殊处理;只需执行一个普通调用:
fscanf(流、%d、&variable)
您可能需要确保
第一行
大于最大行。;)
fscanf(filePointer, "%s\n%d", firstline, &theInteger);