title: Segmentation fault : _IO_feof (fp=0x0) at feof.c:35
body: <p>When I run the output program using gdb from the terminal it always ends with a segmentation fault at this function.
  But running this with eclipse debugger works fine.</p>
  
  <pre><code>ssize_t
  read_from_file(char *arg, unsigned char *buf, size_t max_buf_len) {
    ssize_t result = 0;
    FILE *f = fopen(arg, "r");
    int c;
    while (!feof(f)) {
      size_t bytes_read;
      bytes_read = fread(buf, 1, max_buf_len, f);
      if (ferror(f)) {
        result = -1;
        break;
      }
  
      buf += bytes_read;
      result += bytes_read;
      max_buf_len -= bytes_read;
    }
  
    fclose(f);
    return result;
  }
  </code></pre>
  
  <p>Error message from gdb </p>
  
  <pre><code>[Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  
  Program received signal SIGSEGV, Segmentation fault.
  _IO_feof (fp=0x0) at feof.c:35
  35      feof.c: No such file or directory.
  </code></pre>
  
  <p>I tried using alternatives to <code>feof()</code> Like</p>
  
  <pre><code>while((c = fgetc(file)) != EOF)
  </code></pre>
  
  <p>But I get the same results</p>
  
  <pre><code>[Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  
  Program received signal SIGSEGV, Segmentation fault.
  _IO_getc (fp=0x0) at getc.c:37
  37      getc.c: No such file or directory.
  </code></pre>
  
  <p>Does anyone know where this error is originating from?</p>