How to read an integer which appears right after new line in c?
int fscanf(FILE *stream, const char *format, ...);
I can use fscanf()
to read an integer from file. How can I read an integer that's right after a new开发者_Python百科line?
fscanf()
- look at the example.
Here's what you seem to want:
#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;
}
scan
till a newline and then read an int
. e.g do:
fscanf(filePointer, "%s\n%d", firstline, &theInteger);
The format might need to be modified if there are more things on the line.
精彩评论