I am trying to read a list of comma separated X and Y integers from a file of unknown length and store them into two arrays. When I come to print out my array I am getting values that are not correct at all. The format of the file I am reading in is like this;
60,229
15,221
62,59
96,120
16,97
41,290
52,206
78,220
29,176
25,138
57,252
63,204
94,130
This is the code I've got so far:
#include <stdio.h>
#include <stdlib.h>
int main()
{
//creating a file pointer
FILE *myFile;
//telling the pointer to open the file and what it is called
myFile = fopen("data.txt", "r");
//variables
int size = 0;
int ch = 0;
while(!feof(myFile))
{
ch = fgetc(myFile);
if(ch == '\n') {
size++;
}
}
//check that the right number of lines is shown
printf("size is %d",size);
//create arrays
int xArray[size+1];
int yArray[size+1];
int i,n;
//read each line of two numbers seperated by , into the array
for (i = 0; i <size; i++) {
fscanf(myFile, "%d,%d", &xArray[i], &yArray[i]);
}
//print each set of co-oridantes
for (n = 0; n <size; n++){
printf("x = %d Y = %d\n", xArray[n],yArray[n] );
}
fclose(myFile);
}