简体   繁体   中英

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 a text file of node letters and 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 I am getting double the integer reads from the text file.

我需要在 fscanf 调用中包含一个换行命令。

while(fscanf(file, "%c,%d,%d\n", &office, &x, &y) > 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM