I'm having trouble using the write (), read (), close (), open () functions to copy a text file to a new (previously created) file.
After some research I did get the following code:
#include <fcntl.h>
int main(int argc, char * argv[])
{
char ch;
// FILE *source, *dest;
int n, iDest, iSource;
if(argc!=3){
exit(1);
}
iSource = open(argv[1], O_RDONLY);
if (iSource == -1)
{
exit(1);
}
iDest = open(argv[2], O_WRONLY);
if (iDest == -1)
{
close(iSource);
exit(1);
}
while (n = read(iSource, &ch, 1) > 0){
write(iDest, &ch, 128);
}
close(iSource);
close(iDest);
return 0;
}
Initially there were 2 errors saying that O_RDONLY and O_WONLY were not declared. After a search I decided to use "#include fcntl.h" but I still have problems. This time what happens when I open the destination file is as follows: