Hi ,
I have two files . One source and other destination.
Below are the contents
1302 [12:09] [/cprogs/files] -> cat temp
jimmy
1302 [12:09] [xrakant/cprogs/files] -> cat test
jhony jhony yes papa
I am just copining them using the below code.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <curses.h>
int main(int argc,char *argv[])
{
int sfd,dfd,n = 0;
char buf[5];
memset(buf,0,sizeof(buf));
printf("The file name to opened %s \n",argv[1]);
printf("The file name to copied %s \n",argv[2]);
if (argc == 4 && !strcmp(argv[1],"-f"))
{
printf("need to override the file\n");
}
if (argc == 4 && !strcmp(argv[1],"-f"))
{
printf("need to override the file 1:%s 2:%s 3:%s\n",argv[1],argv[2],argv[3]);
sfd = open(argv[2],O_RDONLY);
dfd = open(argv[3],O_WRONLY | O_CREAT | O_TRUNC);
printf("The open sfd: %d \n",sfd);
printf("The open sfd: %d \n",dfd);
}
else
{
sfd = open(argv[1],O_RDONLY);
dfd = open(argv[2],O_WRONLY | O_CREAT);
printf("The open sfd: %d \n",sfd);
printf("The open sfd: %d \n",dfd);
}
while( (n = read(sfd,buf,sizeof(buf))) && (dfd > 0) && strlen(buf) > 0)
{
printf("The content: %s number of bytest :%d \n",buf,n);
write(dfd,buf,n);
memset(buf,0,sizeof(buf));
}
close(sfd);
close(dfd);
return 0;
}
Output:
eselnts1302 [12:08] [xrakant/cprogs/files] -> ./sysprog1_1 temp test
The file name to opened temp
The file name to copied test
The open sfd: 3
The open sfd: 4
The content: jimmy number of bytest :5
The content:
number of bytest :1
eselnts1302 [12:08] [xrakant/cprogs/files] -> cat test
jimmy
jhony yes papa
I am not sure how the new line character is being added while doing write to destination file.
Thanks
vlrk