study of low level file i/o.

#include <stdio.h>
#include <fcntl.h>

int main(void)
{
char sfilenm[80], tfilenm[80];
int fhs, fht; /* file handles */
char buffer[512]; /* 512 byte buffer */
int bytes_read;
clrscr();
printf("\n enter source file name:");
gets(sfilenm);

fhs = open(sfilenm, O_BINARY | O_RDONLY); // to establish a communication b/w os about file
if(fhs == NULL)
{
printf("\n unable to open source file");
return 1;
}

printf("\n enter target file name:");
gets(tfilenm);

fht = open(tfilenm, O_BINARY | O_WRONLY | O_CREAT);
// when two ot more choices are available then they are combined using bitwise operator(|)

if(fht == NULL)
{
printf("\n unable to open target file");
close(fhs);
return 1;
}

/* read data from source file and write it to target file */
while((bytes_read = read(fhs, buffer, sizeof(buffer))) > 0)

write(fht, buffer, bytes_read);
/*(Here read function takes 3 argument.
1) is to handle the file,
2) address of the buffer
3) number of bytes we want to read.
and in last it will return the number of bytes actually read */


printf("\n file copied successfully");
close(fhs);
close(fht);

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin