c - Ints from text file are being read twice using fscanf to store into arrays -
int main(int argc, char **argv) { file *file = fopen("offices.txt", "r"); char officearray[10]; int ycoordinate[10]; int xcoordinate[10]; int i=0; int x, y; char office; while(fscanf(file, "%c,%d,%d", &office, &x, &y) > 0) { officearray[i] = office; xcoordinate[i] = x; ycoordinate[i] = y; printf("%c,%d,%d \n", officearray[i], xcoordinate[i], ycoordinate[i]); i++; } fclose(file); return 0; }
i have text file of node letters , coordinates:
a,1,1 b,1,5 c,3,7 d,5,5 e,5,1
my output is:
a,1,1 ,1,1 b,1,5 ,1,5 c,3,7 ,3,7 d,5,5 ,5,5 e,5,1 ,5,1
i can't tell why getting double integer reads text file.
i needed include newline command in fscanf call.
while(fscanf(file, "%c,%d,%d\n", &office, &x, &y) > 0)
Comments
Post a Comment