0
I’m having trouble using write(), read(), close(), open() functions to copy a text file to a new file (previously created).
After some research I obtained 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 I had 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:
Thank you very much! But yes my problem was in reading bytes. I just wanted to read one at a time and while copying an old code I forgot to change.
– MarceloCosta
Could you mark the answer as please accept ? is the symbol on the left, right there under the fledgling down
– zentrunix