Hi, I am writing a piece of code that takes copies the contents of one file to another (which is created by the code, the name is given by user as an arguement). I've managed to successfully open and read in the source file, and the program will create the destination file, but it won't write the data out to that file. When I open it, the file is blank/empty and the write function returns an error code.
The source file is just text and I am programming in Linux, it's a requirement for me to use the write function. It's not the buffer size or the count before anyone asks.
I think the error is either with the way that the destination file is opened and permissions, or I'm making a huge oversight in the write function. Any help would be appreciated :)
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd_in, fd_out, bufsize, writeRes;
char genBuffer[50];
fd_in = open(argv[1],O_RDONLY);
fd_out = open(argv[2], O_CREAT | O_EXCL, S_IRWXU);
if (fd_out == -1)
{
printf("Output file already exists\n");
exit(1);
}
bufsize = read(fd_in,&genBuffer,50);
writeRes = write(fd_out,&genBuffer,bufsize);
printf("%d\n",writeRes);
close(fd_in);
close(fd_out);
return 0;
}