SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Programming in C
Objectives


                In this session, you will learn to:
                   Differentiate between high-level and low-level input/output
                   Work with low-level input/output functions
                   Use random access in files




     Ver. 1.0                                                              Slide 1 of 22
Programming in C
Differentiating Between High-Level and Low-Level Input/Output


                In C, files and devices can be accessed by using two
                groups of functions:
                   High-level I/O or stream-level I/O
                   Low-level I/O




     Ver. 1.0                                                          Slide 2 of 22
Programming in C
Difference Between High-level I/O and Low-level I/O


                High-level or Stream-level I/O:
                   Is more flexible and convenient.
                   Hides complexity from the programmer.
                   Is slower.
                Low-level I/O:
                   Provides direct access to files and devices.
                   Is complex (buffer management is to be done by the
                   programmer).
                   Is faster.
                   Uses a file descriptor to track the status of the file.




     Ver. 1.0                                                                Slide 3 of 22
Programming in C
Practice: 8.1


                Which of the following statements is true?
                   In C, there are many interfaces between a program and
                   peripheral devices.
                   A file descriptor is a non-negative integer.
                   When you perform an operation on a file, the system uses the
                   name of the file to identify it.




     Ver. 1.0                                                           Slide 4 of 22
Programming in C
Practice: 8.1 (Contd.)


                Solution:
                 – A file descriptor is a non-negative integer.




     Ver. 1.0                                                     Slide 5 of 22
Programming in C
Uses of Low-Level Input/Output


                Low-level I/O functions are used for:
                   Accessing files and devices directly.
                   Reading binary files in large chunks.
                   Performing I/O operations quickly and efficiently.




     Ver. 1.0                                                           Slide 6 of 22
Programming in C
Working with Low-Level Input/Output Functions


                The low-level I/O system in C provides functions that can be
                used to access files and devices.
                The basic low-level I/O functions are:
                   open()
                   close()
                   read()
                   write()




     Ver. 1.0                                                        Slide 7 of 22
Programming in C
The open() Function


                •   The open() function:
                       Is used to open an existing file or create a new file.
                       Returns a file descriptor for the file name passed to it.
                       Has the following syntax:
                         int open(char *filename, int flags, int perms
                       );




     Ver. 1.0                                                            Slide 8 of 22
Programming in C
The close() Function


                •   The close() function:
                    –   Closes the file that was opened using the open() function.
                    –   Takes the file descriptor as a parameter to close the file.
                    –   Returns 0 on success and -1 in case of an error.
                    –   Has the following syntax:
                         int close(int filedes);




     Ver. 1.0                                                                  Slide 9 of 22
Programming in C
The read() function


                •   The read() function:
                       Reads data from a file.
                       Starts reading a file from the current file position.
                       Has the following syntax:
                       int read (int filedes, char *buffer, int
                       size);




     Ver. 1.0                                                                  Slide 10 of 22
Programming in C
The write() function


                •   The write() function:
                       Enables a user to write contents to a file.
                       Has the following syntax:
                       int write (int filedes, char *buffer, int
                       size);




     Ver. 1.0                                                    Slide 11 of 22
Programming in C
Practice: 8.2


                1. Which of the following statements is true?
                     – At end-of-file, if a function is called repeatedly, it will give error.
                     – In a read() function, the value of zero indicates end-of-file.
                •   What will happen if you do not call the write() function in
                    a loop?




     Ver. 1.0                                                                       Slide 12 of 22
Programming in C
Practice: 8.2 (Contd.)


                Solution:
                 – In a read() function, the value of zero indicates end-of-file.
                 – If you do not call write() function in a loop then the entire
                   data would not be written.




     Ver. 1.0                                                              Slide 13 of 22
Programming in C
Error Handling


                Error Handling:
                 – Some of the low-level I/O functions return error flags when they
                   fail to perform a specified task.
                 – You can find these types of errors using a variable, errno.
                 – The following table lists some values of errno that are
                   common to the open(), close(), read(), and write()
                   functions.

                      errno values                                Description
                EACCES               Specifies that the program has failed to access one of the directories
                                     in the file.
                ENAMETOOLONG         Indicates that the file name is too long.
                ENOSPC               Specifies that the disc is out of space and the file can not be created.
                EIO                  Specifies that there was a hardware error.
                EBADF                Specifies that the file descriptor passed to read, write, or close the
                                     file is invalid.




     Ver. 1.0                                                                                          Slide 14 of 22
Programming in C
Error Handling (Contd.)


                 – There are certain errno values that are specific to the
                   open() function, which are shown with the help of the
                   following table.
                   errno values                                 Description
                EEXIST            Specifies that if File already exists, and O_CREAT and O_EXCL are
                                  set, then opening the file would conflict with the existing file and the
                                  file will not open.
                EISDIR            Specifies that the file is actually a directory.
                ENOENT            Specifies that some of the file components do not exist.
                EMFILE            Specifies that too many files are open.
                EROFS             Specifies that the file is on a read only systembut either one of the
                                  write permissions O_WRONLY, O_RDWR, or O_TRUNC is set.




     Ver. 1.0                                                                                       Slide 15 of 22
Programming in C
Error Handling (Contd.)


                – There are certain errno values that are specific to the
                  write() function, which are shown with the help of the
                  following table.

                       errno values                       Description
                    EFBIG             Specifies that the file will become too large if the
                                      data is written on it.
                    EINTR             Specifies that the write operation is temporarily
                                      interrupted.




     Ver. 1.0                                                                                Slide 16 of 22
Programming in C
Using Random Access Seek in Files


                •   The read and write operations on files are usually
                    sequential in nature.
                •   Random access permits non-sequential file access so that a
                    file can be read or written out of sequence.
                •   Random access in low-level file routines is performed using
                    the lseek() function.




     Ver. 1.0                                                          Slide 17 of 22
Programming in C
The lseek() Function


                •   The lseek() function:
                       Returns the file position, as measured in bytes from the
                       beginning of the file.
                       Has the following syntax:
                        long lseek (int filedes, long offset, int
                        origin);




     Ver. 1.0                                                               Slide 18 of 22
Programming in C
The lseek() Function (Contd.)


                •   The following table lists the various values of the third
                    parameter (origin).
                         Values for origin                       Description

                        SEEK_SET or 0        Specifies that the offset is relative to the
                                             beginning of the file. The offset can only be
                                             positive.
                        SEEK_CUR or 1        Specifies that the offset is relative to the current
                                             position. The offset can be positive or negative.

                        SEEK_END or 2        Specifies that the offset is relative to the end of
                                             the file. The offset can be positive or negative.




     Ver. 1.0                                                                                       Slide 19 of 22
Programming in C
The lseek() Function (Contd.)


                The following table lists some instructions for moving to the
                end or beginning of a file.

                       Instruction                            Function used
                       To get to the end of a file            lseek(filedes,0,2);
                       To return to the beginning of a file   lseek(filedes,0,0);




     Ver. 1.0                                                                       Slide 20 of 22
Programming in C
Summary


               In this session, you learned that:
                – In C, files and devices can be accessed by using high-level I/O
                  or stream-level I/O and low-level I/O.
                – Low-level I/O functions are those, which provide direct access
                  to files and peripheral devices.
                – A file descriptor is a non-negative integer, which is returned by
                  the open() function and is used in read() and write()
                  functions. It tells about the permission to access the file.
                – Low-level input/output functions are used for the following
                  purposes:
                    • Accessing files and devices directly
                    • Reading the binary files in large chunks
                    • Performing I/O operations quickly and efficiently
                – The open() function is used to open or create a file.



    Ver. 1.0                                                              Slide 21 of 22
Programming in C
Summary (Contd.)


               – The close() function is used for closing the file.
               – The read() function reads the existing file up to specified size
                 and stores it into a character array.
               – The write() function writes on the file taking input from a
                 character array up to specified size.
               – Most of the low-level I/O functions throw errors during file
                 handling.
               – errno is the variable that tells about the error occurred during
                 a low-level I/O operation.
               – Files and devices can also be accessed randomly.
               – The lseek() function is used to place the file position to the
                 desired place.
               – The lseek() function do not require to read or write the file
                 for positioning it.



    Ver. 1.0                                                            Slide 22 of 22

Weitere ähnliche Inhalte

Was ist angesagt?

Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...Shinpei Hayashi
 
Reverse code engineering
Reverse code engineeringReverse code engineering
Reverse code engineeringKrishs Patil
 
iFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature ImplementationsiFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature ImplementationsShinpei Hayashi
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGEIJCI JOURNAL
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing filesMukesh Tekwani
 
Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...Shinpei Hayashi
 
LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723Iftach Ian Amit
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programmingMithun DSouza
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
Introduction to Level Zero API for Heterogeneous Programming : NOTES
Introduction to Level Zero API for Heterogeneous Programming : NOTESIntroduction to Level Zero API for Heterogeneous Programming : NOTES
Introduction to Level Zero API for Heterogeneous Programming : NOTESSubhajit Sahu
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,Hossain Md Shakhawat
 
Digital design with Systemc
Digital design with SystemcDigital design with Systemc
Digital design with SystemcMarc Engels
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab MashaelQ
 

Was ist angesagt? (20)

Handout#01
Handout#01Handout#01
Handout#01
 
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
 
Reverse code engineering
Reverse code engineeringReverse code engineering
Reverse code engineering
 
iFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature ImplementationsiFL: An Interactive Environment for Understanding Feature Implementations
iFL: An Interactive Environment for Understanding Feature Implementations
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
Revers engineering
Revers engineeringRevers engineering
Revers engineering
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating System
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script Programming
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Introduction to Level Zero API for Heterogeneous Programming : NOTES
Introduction to Level Zero API for Heterogeneous Programming : NOTESIntroduction to Level Zero API for Heterogeneous Programming : NOTES
Introduction to Level Zero API for Heterogeneous Programming : NOTES
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Digital design with Systemc
Digital design with SystemcDigital design with Systemc
Digital design with Systemc
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 

Andere mochten auch

C programming session 04
C programming session 04C programming session 04
C programming session 04AjayBahoriya
 
C programming session 05
C programming session 05C programming session 05
C programming session 05AjayBahoriya
 
C programming session 02
C programming session 02C programming session 02
C programming session 02AjayBahoriya
 
C programming session 16
C programming session 16C programming session 16
C programming session 16AjayBahoriya
 
C programming session 08
C programming session 08C programming session 08
C programming session 08AjayBahoriya
 
C programming session 10
C programming session 10C programming session 10
C programming session 10AjayBahoriya
 
C programming session 07
C programming session 07C programming session 07
C programming session 07AjayBahoriya
 

Andere mochten auch (7)

C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 

Ähnlich wie C programming session 14

Debug tutorial
Debug tutorialDebug tutorial
Debug tutorialDefri N
 
Java session08
Java session08Java session08
Java session08Niit Care
 
Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3FabMinds
 
Bypassing anti virus scanners
Bypassing anti virus scannersBypassing anti virus scanners
Bypassing anti virus scannersmartacax
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdfvino108206
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
02 fundamentals
02 fundamentals02 fundamentals
02 fundamentalssirmanohar
 
C programming basics
C  programming basicsC  programming basics
C programming basicsargusacademy
 
BIT204 1 Software Fundamentals
BIT204 1 Software FundamentalsBIT204 1 Software Fundamentals
BIT204 1 Software FundamentalsJames Uren
 
Binary translation
Binary translationBinary translation
Binary translationGFI Software
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c languagefarishah
 
Advanced c programming in Linux
Advanced c programming in Linux Advanced c programming in Linux
Advanced c programming in Linux Mohammad Golyani
 
Build process in ST Visual Develop
Build process in ST Visual DevelopBuild process in ST Visual Develop
Build process in ST Visual DevelopGourav Kumar
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageRai University
 

Ähnlich wie C programming session 14 (20)

Debug tutorial
Debug tutorialDebug tutorial
Debug tutorial
 
Java session08
Java session08Java session08
Java session08
 
Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3
 
Bypassing anti virus scanners
Bypassing anti virus scannersBypassing anti virus scanners
Bypassing anti virus scanners
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
Linux basics
Linux basicsLinux basics
Linux basics
 
02 fundamentals
02 fundamentals02 fundamentals
02 fundamentals
 
C programming part1
C programming part1C programming part1
C programming part1
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
BIT204 1 Software Fundamentals
BIT204 1 Software FundamentalsBIT204 1 Software Fundamentals
BIT204 1 Software Fundamentals
 
Java IO
Java IOJava IO
Java IO
 
Binary translation
Binary translationBinary translation
Binary translation
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Advanced c programming in Linux
Advanced c programming in Linux Advanced c programming in Linux
Advanced c programming in Linux
 
Build process in ST Visual Develop
Build process in ST Visual DevelopBuild process in ST Visual Develop
Build process in ST Visual Develop
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c language
 

Kürzlich hochgeladen

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Kürzlich hochgeladen (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

C programming session 14

  • 1. Programming in C Objectives In this session, you will learn to: Differentiate between high-level and low-level input/output Work with low-level input/output functions Use random access in files Ver. 1.0 Slide 1 of 22
  • 2. Programming in C Differentiating Between High-Level and Low-Level Input/Output In C, files and devices can be accessed by using two groups of functions: High-level I/O or stream-level I/O Low-level I/O Ver. 1.0 Slide 2 of 22
  • 3. Programming in C Difference Between High-level I/O and Low-level I/O High-level or Stream-level I/O: Is more flexible and convenient. Hides complexity from the programmer. Is slower. Low-level I/O: Provides direct access to files and devices. Is complex (buffer management is to be done by the programmer). Is faster. Uses a file descriptor to track the status of the file. Ver. 1.0 Slide 3 of 22
  • 4. Programming in C Practice: 8.1 Which of the following statements is true? In C, there are many interfaces between a program and peripheral devices. A file descriptor is a non-negative integer. When you perform an operation on a file, the system uses the name of the file to identify it. Ver. 1.0 Slide 4 of 22
  • 5. Programming in C Practice: 8.1 (Contd.) Solution: – A file descriptor is a non-negative integer. Ver. 1.0 Slide 5 of 22
  • 6. Programming in C Uses of Low-Level Input/Output Low-level I/O functions are used for: Accessing files and devices directly. Reading binary files in large chunks. Performing I/O operations quickly and efficiently. Ver. 1.0 Slide 6 of 22
  • 7. Programming in C Working with Low-Level Input/Output Functions The low-level I/O system in C provides functions that can be used to access files and devices. The basic low-level I/O functions are: open() close() read() write() Ver. 1.0 Slide 7 of 22
  • 8. Programming in C The open() Function • The open() function: Is used to open an existing file or create a new file. Returns a file descriptor for the file name passed to it. Has the following syntax: int open(char *filename, int flags, int perms ); Ver. 1.0 Slide 8 of 22
  • 9. Programming in C The close() Function • The close() function: – Closes the file that was opened using the open() function. – Takes the file descriptor as a parameter to close the file. – Returns 0 on success and -1 in case of an error. – Has the following syntax: int close(int filedes); Ver. 1.0 Slide 9 of 22
  • 10. Programming in C The read() function • The read() function: Reads data from a file. Starts reading a file from the current file position. Has the following syntax: int read (int filedes, char *buffer, int size); Ver. 1.0 Slide 10 of 22
  • 11. Programming in C The write() function • The write() function: Enables a user to write contents to a file. Has the following syntax: int write (int filedes, char *buffer, int size); Ver. 1.0 Slide 11 of 22
  • 12. Programming in C Practice: 8.2 1. Which of the following statements is true? – At end-of-file, if a function is called repeatedly, it will give error. – In a read() function, the value of zero indicates end-of-file. • What will happen if you do not call the write() function in a loop? Ver. 1.0 Slide 12 of 22
  • 13. Programming in C Practice: 8.2 (Contd.) Solution: – In a read() function, the value of zero indicates end-of-file. – If you do not call write() function in a loop then the entire data would not be written. Ver. 1.0 Slide 13 of 22
  • 14. Programming in C Error Handling Error Handling: – Some of the low-level I/O functions return error flags when they fail to perform a specified task. – You can find these types of errors using a variable, errno. – The following table lists some values of errno that are common to the open(), close(), read(), and write() functions. errno values Description EACCES Specifies that the program has failed to access one of the directories in the file. ENAMETOOLONG Indicates that the file name is too long. ENOSPC Specifies that the disc is out of space and the file can not be created. EIO Specifies that there was a hardware error. EBADF Specifies that the file descriptor passed to read, write, or close the file is invalid. Ver. 1.0 Slide 14 of 22
  • 15. Programming in C Error Handling (Contd.) – There are certain errno values that are specific to the open() function, which are shown with the help of the following table. errno values Description EEXIST Specifies that if File already exists, and O_CREAT and O_EXCL are set, then opening the file would conflict with the existing file and the file will not open. EISDIR Specifies that the file is actually a directory. ENOENT Specifies that some of the file components do not exist. EMFILE Specifies that too many files are open. EROFS Specifies that the file is on a read only systembut either one of the write permissions O_WRONLY, O_RDWR, or O_TRUNC is set. Ver. 1.0 Slide 15 of 22
  • 16. Programming in C Error Handling (Contd.) – There are certain errno values that are specific to the write() function, which are shown with the help of the following table. errno values Description EFBIG Specifies that the file will become too large if the data is written on it. EINTR Specifies that the write operation is temporarily interrupted. Ver. 1.0 Slide 16 of 22
  • 17. Programming in C Using Random Access Seek in Files • The read and write operations on files are usually sequential in nature. • Random access permits non-sequential file access so that a file can be read or written out of sequence. • Random access in low-level file routines is performed using the lseek() function. Ver. 1.0 Slide 17 of 22
  • 18. Programming in C The lseek() Function • The lseek() function: Returns the file position, as measured in bytes from the beginning of the file. Has the following syntax: long lseek (int filedes, long offset, int origin); Ver. 1.0 Slide 18 of 22
  • 19. Programming in C The lseek() Function (Contd.) • The following table lists the various values of the third parameter (origin). Values for origin Description SEEK_SET or 0 Specifies that the offset is relative to the beginning of the file. The offset can only be positive. SEEK_CUR or 1 Specifies that the offset is relative to the current position. The offset can be positive or negative. SEEK_END or 2 Specifies that the offset is relative to the end of the file. The offset can be positive or negative. Ver. 1.0 Slide 19 of 22
  • 20. Programming in C The lseek() Function (Contd.) The following table lists some instructions for moving to the end or beginning of a file. Instruction Function used To get to the end of a file lseek(filedes,0,2); To return to the beginning of a file lseek(filedes,0,0); Ver. 1.0 Slide 20 of 22
  • 21. Programming in C Summary In this session, you learned that: – In C, files and devices can be accessed by using high-level I/O or stream-level I/O and low-level I/O. – Low-level I/O functions are those, which provide direct access to files and peripheral devices. – A file descriptor is a non-negative integer, which is returned by the open() function and is used in read() and write() functions. It tells about the permission to access the file. – Low-level input/output functions are used for the following purposes: • Accessing files and devices directly • Reading the binary files in large chunks • Performing I/O operations quickly and efficiently – The open() function is used to open or create a file. Ver. 1.0 Slide 21 of 22
  • 22. Programming in C Summary (Contd.) – The close() function is used for closing the file. – The read() function reads the existing file up to specified size and stores it into a character array. – The write() function writes on the file taking input from a character array up to specified size. – Most of the low-level I/O functions throw errors during file handling. – errno is the variable that tells about the error occurred during a low-level I/O operation. – Files and devices can also be accessed randomly. – The lseek() function is used to place the file position to the desired place. – The lseek() function do not require to read or write the file for positioning it. Ver. 1.0 Slide 22 of 22

Hinweis der Redaktion

  1. Begin the session by explaining the objectives of the session.
  2. Give examples where low-level and I/O can be used.
  3. Use this slide to test the student’s understanding on high-level and low-level I/O.
  4. Give a scenario and ask the students which type of I/O they would use in the situation. Also, ask them to justify their answers.
  5. Tell the students that the value returned by the open() function can be used to check whether a file has been opened successfully.
  6. The close() function releases the resources occupied. It also flushes out the data from the streams before closing them.
  7. If successful, the read() functions returns the number of characters read.
  8. If successful, the write function returns the number of bytes written into the file. This number should be equal to the size parameter of the write function.
  9. Use this slide to test the student’s understanding on low-level I/O functions.
  10. Use this and the next slide to summarize the session.