Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h>
# include <fcntl.h>
# include <stdlib.h>
# include <sys/stat.h>

int main(int argc, char *argv[])
{
     int fRead, fPadded, padVal;
     int btRead;
     int BUFFSIZE = 512;
     char buff[BUFFSIZE];

     if (argc != 4)
     {
          printf ("Please provide all of the following: names of original and padded files, and a pad value.\n");
          return 0;
     }

     if (fRead =open (argv[1], O_RDONLY) ==-1)
     {
          printf ("An error has occurred opening the file.\nPlease make sure file names are correct.\n");
          return 0;
     }
     else
     {
          close (fRead);
          if (fPadded =creat (argv[2], S_IRWXU) ==-1 )
          {
               printf ("An error has occurred writing the file.\n");
               return 0;
          }
          else
          {
               close (fPadded);

               if (padVal =atol(argv[3]) ==-1)
               {
                    printf ("An error has occurred with pad value.\nPlease make sure the number is correct.\n");
                    return 0;
               }
               else
               {
                    if (padVal<0 || padVal>(2^32-1))
                    {
                         printf ("The pad value is out of range, it should be greater than 0 and less than 4294967295.\n");
                    }
                    else
                    {
                         while ((btRead = read (fRead, buff, BUFFSIZE))>0)
          if (write (fPadded, buff, btRead) != btRead)
                                  printf ("An error has occurred while copying file\n");
                              if (btRead <0)
                                  printf ("An error has occurred while reading file\n");

                         return 1;
                    }
               }
          }
     }
}

I think every part of the program works fine except for the highlighted part. For some reason, c fails to read a file and btRead is assigned a -1 value. Of course, write function does not work either, because buff is probably empty. What could be the problem? Thanks in advance.

You can't read from or write to a file descriptor after you close it. You need to remove the close() statements:

           .
           .
           .
          close (fRead);  <--- 
          if (fPadded =creat (argv[2], S_IRWXU) ==-1 )
          {
                    printf ("An error has occurred writing the file.\n");
                    return 0;
          }
          else
          {
             close (fPadded);   <---
               .
               .
               .

Before the close() happens, his biggest problem is his syntax:

if (fRead =open (argv[1], O_RDONLY) ==-1)

is completely wrong, and he's done it in several places.

== has higher precedence than =, so this is parsed as:

if (fRead = (open (argv[1], O_RDONLY) ==-1))

try

if ((fRead = open (argv[1], O_RDONLY)) ==-1) 

and use this method throughout your code.

Also, don't use the ato* functions, but strto* instead, because they set errno.

Doh! I totally missed that.

Just another example of why you should never, ever perform assignments within a conditional clause - it's all too easy to be misleading. If the code had been written as follows the bug would never could have been introduced:

fRead = open( argv[ 1 ], O_RDONLY );
if ( fRead == -1 )
{
    printf( "...." );
    return 0;
}