This program reads the contents of file1.txt and copies it to file2.txt in three different ways: copying the whole file, copying from a specific position to the end, or copying a specific region. It uses functions like open(), read(), write(), lseek() to perform the file operations. The user is prompted to choose which copying method to use. The files are then closed before the program ends.
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
Read write program
1. Read - Write program Presentation by Salam Ali Ahasan ID:090100043
2.
3. The code and explains #include<unistd.h> // for read () ,write() and lseek() functions #include<fcntl.h>// for creat() and open() functions #include<stdio.h>//for standard i/o int main() { int fd1,fd2;// to hold file decriptor value char buffer;//0-st-input,1-st-output and 2-st-error int fileSize=0,i=0, choice1=0, choice2=0, readchoice=0; // open and/or creat files // check file1.txt is not exist or file2.txt (existing with not valid permission or create with permission)..)
4. The code and explains if(((fd1=open("file1.txt", O_RDONLY))==-1)||((fd2=open("file2.txt", O_CREAT | O_APPEND | O_WRONLY , 0700))==-1)) { printf("File Problem"); //when true show file problem and exit program exit(1); } //when valid files; check read filesize while(read(fd1, &buffer, 1)>0)// read one byte from file1.txt and store to buffer { fileSize++;// total file size in bytes }
5.
6.
7. if(fileSize<=0)// message for empty file or file size { printf(" this file is empty"); } else { printf(" there are 1 to %lu characters in the file ",fileSize-1); } // user choice to read data whole file, partially or specific portion of file printf(" input 1 to read whole file: input 2 to read from specific position to last: input 3 to read a specific range: "); scanf("%d",&readchoice);
8. switch(readchoice) { // copy character by character whole file case 1: choice1=2; choice2=fileSize; while(choice1<=choice2) { lseek(fd1,choice1,SEEK_SET); read(fd1, &buffer, 1); write(fd2, &buffer,1); choice1++; } break;
9. lseek Function Every open file has an associated "current file offset," normally a non-negative integer that measures the number of bytes from the beginning of the file. Read and write operations normally start at the current file offset and cause the offset to be incremented by the number of bytes read or written. By default, this offset is initialized to 0 when a file is opened, unless the O_APPEND option is specified. An open file's offset can be set explicitly by calling lseek. #include <unistd.h> off_t lseek(int filedes , off_t offset , int whence ); Returns: new file offset if OK, 1 on error
10.
11.
12.
13. // copy specific byte to end of the file case 2: printf(" from where do you want to copy?"); scanf("%d",&choice1); if(choice1>=fileSize) printf("you must input less than file size"); else while(choice1<=fileSize) { lseek(fd1,choice1,SEEK_SET); read(fd1, &buffer, 1); write(fd2, &buffer,1); choice1++; } break;
14. // copy a special region of the file. case 3: printf(" starting position for copy:"); scanf("%d",&choice1); printf(" ending position for copy?"); scanf("%d",&choice2); if(choice1>=fileSize || choice2>=fileSize ) printf("you must input less than file size"); else while(choice1<=choice2) { lseek(fd1,choice1,SEEK_SET); read(fd1, &buffer, 1); write(fd2, &buffer,1); choice1++; } break;
15. default : printf(" you miss to choice"); } // close both files close(fd1); close(fd2); }