arj
arj

Reputation: 23

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.

Upvotes: 1

Views: 115

Answers (1)

arj
arj

Reputation: 23

I needed to include a newline command in my fscanf call.

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

Upvotes: 1

Related Questions