1

Working on a project where I have to have a file that is generated numbers. First line is a generated int. Followed by a floats (separate lines). (I'm doing it separate lines because I feel it makes more sense as I have to read it two different ways for the bin packing problems which I need this for... Like one way of reading one at a time and another storing it in an array.. But want to get this down first) Getting a seg fault when I try to read my file for a float after reading an int. Edit: Error occurs in readOffline.

int randomFunction()
{
  FILE *fp;
  int i;
  fp = fopen("theItems.txt", "w" );
  if (fp == NULL) 
     printf("Error: file can't be opened.\n");

  srand(time(NULL) );
  int random_number = rand();
  printf("Random Number %d\n", random_number);
  fprintf(fp,"%d",random_number);
  fclose(fp);
  fp = fopen("theItems.txt", "a");
  int numberOfItems = rand();
  printf("NumberOfItems: %d\n",numberOfItems);
  for(i = 0; i < 10; i++)
  {
     fp = fopen("theItems.txt", "a");
     float number = (float)rand()/(float)(RAND_MAX);
     fprintf(fp,"%f",number);
     fprintf(fp,"%s", "\n");
     fclose(fp);
   }
   return numberOfItems;
}

void readOffline( int numberOfItems)
{
  FILE *fp;
  int n = 0,i;
  float nu = 0.00;
  fp = fopen("theItems.txt", "r");
  if (fp == NULL) 
    printf("Error: file can't be opened.\n");

  fseek(fp,SEEK_SET,0);
  fscanf(fp,"%d",&n);
  printf("Number read: %d\n", n);
  float array[numberOfItems];
   // for(i = 0; i < 3; i++)
   // {
 fscanf(fp,"%f",&nu);
  // array[i] = nu;
   // }   
 fclose(fp);
 printf("Int:%d\n", n);
 int j;
 // for(j = 0; j < 3; j++)  
 // printf("Float Number:%f\n", array[j]);
}  
int main()
{  

   int numberOfItems = randomFunction();
   readOffline(numberOfItems);
   return 0;
}

Just trying to get an understanding why it causes a seg error when I // it out I can get it to read my int but sometimes it isn't the right int read. But yeah. Please let me know if I need any more details or need to be more clear anywhere

anchorman
  • 27
  • 4

2 Answers2

0

I would say that depending on the particular compiler that you are using, then this could be a problem in setting up the actual array. This is (as an example) discussed in Variable Sized Arrays vs calloc in C From the discussions, you should use calloc and free. Another point is that you need to make sure that your value is greater than 3 and not too big. Since the array is only in the local scope of readOffline(), you should not connect it to the variable numberOfItems.

float array[3];
   for(i = 0; i < 3; i++)
   {
       fscanf(fp,"%f",&nu);
       array[i] = nu;
   }   
Community
  • 1
  • 1
sabbahillel
  • 4,357
  • 1
  • 19
  • 36
0

You have multiple issues in your code:

  • You open the output file multiple times in randomFunction(), you even leak a stream handle and leave it open.

  • You do not exit the function when fopen() returns NULL. The rest of the code invokes undefined behavior if fp == NULL.

  • The same problem is present in readOffline(): if fp == NULL, you should return from the function immediately.

  • you do not output a linefeed after the first random number in the output file.

  • you always output 10 random numbers.

  • most importantly: the random number returned by the randomFunction() is potentially huge, allocating an array with local storage larger than a few megabytes is likely to cause undefined behavior. Try and reduce the maximum random number of values.

Here is a proposed correction:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int randomFunction(void) {
    FILE *fp;
    int i;
    fp = fopen("theItems.txt", "w");
    if (fp == NULL) {
        printf("Error: file can't be opened.\n");
        return -1;
    }

    srand(time(NULL));
    int random_number = rand();
    printf("Random Number %d\n", random_number);
    fprintf(fp, "%d\n", random_number);

    int numberOfItems = 1 + rand() % 100; /* between 1 and 100 */
    printf("NumberOfItems: %d\n", numberOfItems);
    for (i = 0; i < numberOfItems; i++) {
        float number = rand() / (float)(RAND_MAX);
        fprintf(fp, "%f\n", number);
    }
    fclose(fp);
    return numberOfItems;
}

void readOffline(int numberOfItems) {
    FILE *fp;
    int n = 0, i;
    fp = fopen("theItems.txt", "r");
    if (fp == NULL) {
        printf("Error: file can't be opened.\n");
        return;
    }

    fscanf(fp, "%d", &n);
    printf("Number read: %d\n", n);

    float array[numberOfItems];
    for (i = 0; i < numberOfItems; i++) {
        if (fscanf(fp, "%f", &array[i]) != 1)
            break;
    }
    fclose(fp);
    printf("Int:%d\n", n);
    for (int j = 0; j < i; j++) {
        printf("Float Number %d: %f\n", j, array[j]);
    }
}  

int main(void) {  
    int numberOfItems = randomFunction();
    readOffline(numberOfItems);
    return 0;
}

Note that I kept your semantics: the random number at the start of the file is not the number of floating point values that follow. I suspect it should be?

chqrlie
  • 131,814
  • 10
  • 121
  • 189