c - I/O using system calls -
i have file bisht.txt , pranav.txt. want copy content bisht.txt pranav.txt. read system call working because printed str right after it, printed exact content of file when check contents of destination file pranav.txt, chinese language written no. of characters same no. of bytes mentioned in write system call. please help!
#include<unistd.h> #include<fcntl.h> #include<stdio.h> main() { //file descriptor rdes , wdes /*open file pranav.txt in write-only mode, o_creat creates file if not exist , open bisht.txt in read-only mode*/ int rdes = open("bisht.txt",o_rdonly); int wdes = open("pranav.txt", o_creat | o_wronly); char str[10]; if(wdes!=-1 && rdes!=-1) { //to read read(rdes,&str,10); //to write on file write(wdes,&str,10); } else { //print "error" on screen write(2,"file_opening_error",18); } close(wdes); close(rdes); }
you not using return read write bytes. read
function returns number of bytes read:
int cnt; cnt=read(rdes,&str,sizeof(str)); if(cnt>0) { write(wdes,&str,cnt); }
and technically write may return short count should write in loop until bytes written, or -1
indicates error. though local storage device short-write never happen.
Comments
Post a Comment