title: File opened successfully but reads result in "Bad file descriptor" error and stat shows filesize zero
body: <p>I'm working on an assignment where we're supposed to crypt-analyze a PDF that had been encrypted with a poor encryption algorithm.  </p>
  
  <p>The code supplied by the prof creates the encrypted file with <code>fd=open(filename, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR)</code>.</p>
  
  <p>In my code to attempt decryption I open that file with <code>fd_in=open(file, O_RDONLY)</code>.</p>
  
  <p>The problem is that when I try to read in from the file I'm getting a "Bad file descriptor" error. I used stat to attempt to get more information about what the file descriptor "thought" about the file that had been opened and it shows that the file is of length 0 when it is actually a few hundred KB.</p>
  
  <p>The debug code I'm using is:</p>
  
  <pre><code>if (0 &gt; (len = read(fd_in, (char*)&amp;read_buff, BITE))) {    // BITE is defined as 8
    printf("Error occured grabbing first bite of %s.\n", file);
    printf("%s.\n", strerror(errno));
  
    struct stat fileStat;
    int stat = fstat(fd_in, &amp;fileStat);
  
    printf("fstat returned: %d.\n", stat);      // Consistently printing 0
    printf("Information for %s\n",file);
    printf("---------------------------\n");
    printf("File Size: \t\t%d bytes\n",fileStat.st_size);
    printf("Number of Links: \t%d\n",fileStat.st_nlink);
    printf("File inode: \t\t%d\n",fileStat.st_ino);
  
    printf("File Permissions: \t");
    printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
    printf( (fileStat.st_mode &amp; S_IRUSR) ? "r" : "-");
    printf( (fileStat.st_mode &amp; S_IWUSR) ? "w" : "-");
    printf( (fileStat.st_mode &amp; S_IXUSR) ? "x" : "-");
    printf( (fileStat.st_mode &amp; S_IRGRP) ? "r" : "-");
    printf( (fileStat.st_mode &amp; S_IWGRP) ? "w" : "-");
    printf( (fileStat.st_mode &amp; S_IXGRP) ? "x" : "-");
    printf( (fileStat.st_mode &amp; S_IROTH) ? "r" : "-");
    printf( (fileStat.st_mode &amp; S_IWOTH) ? "w" : "-");
    printf( (fileStat.st_mode &amp; S_IXOTH) ? "x" : "-");
    printf("\n\n");
  
    return 1;
  </code></pre>
  
  <p>}</p>
  
  <p>The result I'm getting is:</p>
  
  <pre><code>Error occured grabbing first bite of enc.pdf.
  Bad file descriptor.
  Information for enc.pdf
  ---------------------------
  File Size:      0 bytes
  Number of Links:    1
  File inode:         16441996
  File Permissions:   -rw-------
  </code></pre>
  
  <p>ls reports the file as</p>
  
  <pre><code>-rw-------  1 matt  matt   157887 Oct 29 03:01 enc.pdf
  </code></pre>
  
  <hr>
  
  <p>The code related to opening the file:</p>
  
  <pre><code>int fd_in=open(file, O_RDONLY);
  if(fd_in&lt;0) {
     printf("Failed to open the input file %s.\n", file);
     return 1;
  } else {
      printf("File open, descriptor is: %d.\n", fd_in);
  }
  </code></pre>
  
  <p>This has been consistently printing out the value 3 for the filed descriptor.</p>
  
  <p>There were some questions about read_buff.  The encryption/decryption process involves XORing the values that are read.  Because of this the buffer is declared as a <code>unsigned long long</code> and in order to read into it I take the address and cast it to (char*).  This tactic is straight out of the prof's code for creating the encrypted file.</p>
  
  <p>I even added an else with a printf to verify the file descriptor was coming out valid.  At the moment it seems to be conistently <code>3</code> which is definitely not <code>-1</code></p>