title: How do you properly use the fstat() function and what are its limits?
body: <p>So I am very new to C. I have done a lot of programming in Java and am finding it very difficult to learn C. </p>
  
  <p>Currently I am assigned to read in a file from our terminal window, which will contain a list of integers. From this list we must read the values and calculate the average, I believe I have done this correctly. </p>
  
  <p>My only problem is I do not understand how to use the fstat() correctly, I read the man page but am still having a hard time understanding. In my code below, I am wanting to use fstat() to find the size of the file being read so I can then allocate the correct amount of space for my array where I store the values read off the input file. I just need to know the proper usage and syntax of fstat() and from there I believe I can make significant progress. Thanks in advance!</p>
  
  <pre><code>    char *infile;
      int fileSize;
      int fd;
      int i;
      int j;
      int temp;
      int sum = 0;
      int average;
      /* enforce proper number of arguments
      */
      if (argc != 1)
      {
              fprintf(stderr, "Too many arguments.\n");
              exit(1);
      }
  
      infile = argv[1];
  
      //open file
      fd = open(infile, O_RDONLY);
  
      //exit if open fails
      assert (fd &gt; -1);
  
      //find size of file
      fileSize = fstat(fd, blksize_t st_blksize); 
  
      //fine perfect size for array
      temp =  malloc(temp * sizeof(int));
  
      //create file of perfect size
      int intArray[temp];
  
      //scan integers into our array
      for (i = 0; i &lt; fileSize; i++)
      {
              fscanf(infile, "%d", intArray[i]);
      }
      fclose(fd);
  
      //add all integers into the array up
      for (j = 0; j &lt; fileSize; j++);
       {
              sum = sum + intArray[j];
       }
  
      //calculat average
      average = (sum)/fileSize;
  
      printf("Number of numbers averaged: %d\n Average of numbers: %d\n", fileSize, average);
  
      if ( close(fd) == -1 )
       {
               fprintf(stderr, "error closing file -- quitting");
               exit(1);
       }
  
  return 0;
  </code></pre>
  
  <p>}</p>