0

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);
}
M.K
  • 1,464
  • 2
  • 24
  • 46
Dave
  • 41
  • 1
  • 5
  • You need to reset the file pointer to the start of file. Do `rewind(myFile)` before first `for` loop. – H.S. Oct 19 '18 at 08:57
  • [why not use while (feof) in c](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – H.S. Oct 19 '18 at 09:20

1 Answers1

2

Ooh! This is a horrible problem.

You've got this code to make sure that your file is the right size; a sort of "debug check".

//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);

But actually this is the cause of the bug, as it's "using up" the whole file, meaning that the values are never loaded from the file and you just get whatever was stored in that memory beforehand.

To fix this, either remove your checking code or add this line at the end of it (either before or after the printf):

rewind(myFile);

This seeks back to the start of the file, so you can read the actual data out of it. You could also use:

fseek(myFile, 0, SEEK_SET);

which does the same thing.


While I'm at it, I'll just fix your scanf line:

        fscanf(myFile, "%d,%d\n", &xArray[i], &yArray[i]);

You need a character at the end of the format string, because there's a '\n' in between the lines.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
  • Perfect, such a simple solution, the size element is just there because I do not know how many lines of co-ordinates there may be in the file, if that makes sense. Thank you ! – Dave Oct 19 '18 at 09:03
  • @Dave Oh; I thought that was there for debugging! It's a slightly less horrible problem than I thought, then. In future, try creating a [mcve] before posting on Stack Overflow; you might've found the problem that way. – wizzwizz4 Oct 19 '18 at 09:04