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

时间:2011-09-10 04:07:52

标签: c scanf

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

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

2 个答案:

答案 0 :(得分:2)

fscanf() - 看一下这个例子。

这就是你想要的东西:

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

答案 1 :(得分:0)

scan直到换行,然后阅读int。例如:

fscanf(filePointer, "%s\n%d", firstline, &theInteger); 

如果线路上有更多东西,可能需要修改格式。