# 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.