Hi, I am using a C program to open and scan a file containing rows of x,y coordinates.
But I need to use the same program to open different files having different number of rows.Therefore, I cant fix a constant number for the array.
This is what I have used:

int i,count=1;
FILE *fp;
float x[count],y[count],multFact;

while (feof(fp)==0)
       {
         count++;
         for(i=0;i<count;i++)
          {
             fscanf(fp,"%f %f",&x[i],&y[i]);
          }

But, the output screen just remains blank.
Can anyone help me with the problem with my logic.

Did you forget to open the file? ;)

fp = fopen("myfileName", "r");
if(fp==NULL) {
  printf("Unable to open the file\n");
  return 1;  //or use exit() if needed
}

feof() may repeat your last value, check your return type from fscanf() and if it's less than it should be, break out of the input from the file loop.

You know that so far, your code hasn't printed anything onto the screen. ;) That guarantees a blank screen from your program.

As far as the number of rows problem. What range of numbers are you dealing with?

Did you forget to open the file? ;)

fp = fopen("myfileName", "r");
if(fp==NULL) {
  printf("Unable to open the file\n");
  return 1;  //or use exit() if needed
}

feof() may repeat your last value, check your return type from fscanf() and if it's less than it should be, break out of the input from the file loop.

You know that so far, your code hasn't printed anything onto the screen. ;) That guarantees a blank screen from your program.

As far as the number of rows problem. What range of numbers are you dealing with?

Ah yes, I had forgotten to type in the file opening part.
And also I have included only the file reading part of the code.

The no. of rows varies from 35 to 60.

>The no. of rows varies from 35 to 60.
Then set the size of your arrays to 60. If the number of rows were completely indeterminate (eg. as few as zero and in excess of millions) you would be better served by either a dynamic data structure or an algorithm that doesn't need to store all of the rows simultaneously. But there's really no point optimizing for a range of 35 to 60, just use the simplest solution and refactor later if the requirements change.

>The no. of rows varies from 35 to 60.
Then set the size of your arrays to 60. If the number of rows were completely indeterminate (eg. as few as zero and in excess of millions) you would be better served by either a dynamic data structure or an algorithm that doesn't need to store all of the rows simultaneously. But there's really no point optimizing for a range of 35 to 60, just use the simplest solution and refactor later if the requirements change.

If I give the array size as 60 and if there are only 35 rows then for the rest of the array size junk value gets printed.
Is there a way to find the no of rows while the program is reading the file for the first time?

If I give the array size as 60 and if there are only 35 rows then for the rest of the array size junk value gets printed.
Is there a way to find the no of rows while the program is reading the file for the first time?

Are you really certain you can't figure out how to fix the junk values?

If I give the array size as 60 and if there are only 35 rows then for the rest of the array size junk value gets printed.

You should be using an actual row count anyway, not the size of the array. Initialize an int variable to zero, then increment it each time you successfully read a row into the array. Simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.