I want to read integers from a file like this:
9 5 1847
6 9 5
87
2 48 1
7 1 5 4 2
6 17 8
95
4 6 8
5192 7 4
For example 1,8,4,7 are to be taken as different numbers.
I tried this way:
#define MAXLINE 1024
void readFile(FILE *file){
char line [MAXLINE];
int j;
while(fgets(line,MAXLINE,file)!=NULL){
for(j=0;j<9;j++){
int a = atoi(&line[j]);
printf("%d \n",a);
}
}
}
But it is reading 1847 as only one number.
The
%1dpart of the format reads a single digit (but also skips blanks). The%nis in C89 and reports the position where the scan is in the string; it is not counted as a conversion (hence the comparison with 1).