SlideShare a Scribd company logo
1 of 16
Read - Write program Presentation by  Salam Ali Ahasan ID:090100043
About this program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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)..)
The code and explains if(((fd1=open(&quot;file1.txt&quot;, O_RDONLY))==-1)||((fd2=open(&quot;file2.txt&quot;, O_CREAT | O_APPEND | O_WRONLY , 0700))==-1)) { printf(&quot;File Problem&quot;);  //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 }
Open Function  ,[object Object],We show the third argument as ..., which is the ISO C way to specify that the number and types of the remaining arguments may vary. For this function, the third argument is used only when a new file is being created, as we describe later. We show this argument as a comment in the prototype. The  pathname  is the name of the file to open or create. This function has a multitude of options, which are specified by the  oflag  argument. This argument is formed by ORing together one or more of the following constants from the <fcntl.h> header:
Some ORings  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
if(fileSize<=0)// message for empty file or file size { printf(&quot; this file is empty&quot;); } else { printf(&quot; there are 1 to  %lu characters in the file  &quot;,fileSize-1); } // user choice to read data whole file, partially or specific portion of file printf(&quot; input 1 to read whole file: input 2 to read from specific position to last: input 3 to read a specific range: &quot;); scanf(&quot;%d&quot;,&readchoice);
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;
lseek Function Every open file has an associated &quot;current file offset,&quot; 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
Whence ,[object Object],[object Object],[object Object],[object Object]
read Function ,[object Object],[object Object],[object Object],[object Object]
write Function ,[object Object],[object Object],[object Object],[object Object]
// copy specific byte to end of the file case 2: printf(&quot; from where do you want to copy?&quot;); scanf(&quot;%d&quot;,&choice1); if(choice1>=fileSize) printf(&quot;you must input less than file size&quot;); else  while(choice1<=fileSize) { lseek(fd1,choice1,SEEK_SET); read(fd1, &buffer, 1); write(fd2, &buffer,1); choice1++; } break;
// copy a special region of the file. case 3: printf(&quot; starting position for copy:&quot;); scanf(&quot;%d&quot;,&choice1); printf(&quot; ending position for copy?&quot;); scanf(&quot;%d&quot;,&choice2); if(choice1>=fileSize || choice2>=fileSize ) printf(&quot;you must input less than file size&quot;); else while(choice1<=choice2) { lseek(fd1,choice1,SEEK_SET); read(fd1, &buffer, 1); write(fd2, &buffer,1); choice1++; } break;
default : printf(&quot; you miss to choice&quot;); } // close both files close(fd1); close(fd2); }
Thanks for your times  See you, bye.

More Related Content

What's hot (20)

File handling in C
File handling in CFile handling in C
File handling in C
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File handling C program
File handling C programFile handling C program
File handling C program
 
Satz1
Satz1Satz1
Satz1
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
File in c
File in cFile in c
File in c
 
Filehandling
FilehandlingFilehandling
Filehandling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
File handling in c
File handling in cFile handling in c
File handling in c
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
C language updated
C language updatedC language updated
C language updated
 
working with files
working with filesworking with files
working with files
 
File Pointers
File PointersFile Pointers
File Pointers
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)
 

Similar to Read write program

file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.pptyuvrajkeshri
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptxChenamPawan
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'eShikshak
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File ConceptsANUSUYA S
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 

Similar to Read write program (20)

Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
file.ppt
file.pptfile.ppt
file.ppt
 
File handling in c
File handling in cFile handling in c
File handling in c
 
18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
File management in C++
File management in C++File management in C++
File management in C++
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Unit 8
Unit 8Unit 8
Unit 8
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 

Recently uploaded

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Recently uploaded (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"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(&quot;file1.txt&quot;, O_RDONLY))==-1)||((fd2=open(&quot;file2.txt&quot;, O_CREAT | O_APPEND | O_WRONLY , 0700))==-1)) { printf(&quot;File Problem&quot;); //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(&quot; this file is empty&quot;); } else { printf(&quot; there are 1 to %lu characters in the file &quot;,fileSize-1); } // user choice to read data whole file, partially or specific portion of file printf(&quot; input 1 to read whole file: input 2 to read from specific position to last: input 3 to read a specific range: &quot;); scanf(&quot;%d&quot;,&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 &quot;current file offset,&quot; 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(&quot; from where do you want to copy?&quot;); scanf(&quot;%d&quot;,&choice1); if(choice1>=fileSize) printf(&quot;you must input less than file size&quot;); 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(&quot; starting position for copy:&quot;); scanf(&quot;%d&quot;,&choice1); printf(&quot; ending position for copy?&quot;); scanf(&quot;%d&quot;,&choice2); if(choice1>=fileSize || choice2>=fileSize ) printf(&quot;you must input less than file size&quot;); else while(choice1<=choice2) { lseek(fd1,choice1,SEEK_SET); read(fd1, &buffer, 1); write(fd2, &buffer,1); choice1++; } break;
  • 15. default : printf(&quot; you miss to choice&quot;); } // close both files close(fd1); close(fd2); }
  • 16. Thanks for your times See you, bye.