SlideShare a Scribd company logo
Slide 1 of 22Ver. 1.0
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
Slide 2 of 22Ver. 1.0
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
Slide 3 of 22Ver. 1.0
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.
Slide 4 of 22Ver. 1.0
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.
Slide 5 of 22Ver. 1.0
Programming in C
Practice: 8.1 (Contd.)
Solution:
A file descriptor is a non-negative integer.
Slide 6 of 22Ver. 1.0
Programming in C
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.
Uses of Low-Level Input/Output
Slide 7 of 22Ver. 1.0
Programming in C
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()
Working with Low-Level Input/Output Functions
Slide 8 of 22Ver. 1.0
Programming in C
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
);
The open() Function
Slide 9 of 22Ver. 1.0
Programming in C
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);
The close() Function
Slide 10 of 22Ver. 1.0
Programming in C
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);
The read() function
Slide 11 of 22Ver. 1.0
Programming in C
The write() function:
Enables a user to write contents to a file.
Has the following syntax:
int write (int filedes, char *buffer, int
size);
The write() function
Slide 12 of 22Ver. 1.0
Programming in C
Practice: 8.2
1. Which of the following statements is true?
a. At end-of-file, if a function is called repeatedly, it will give error.
b. In a read() function, the value of zero indicates end-of-file.
2. What will happen if you do not call the write() function in
a loop?
Slide 13 of 22Ver. 1.0
Programming in C
Solution:
1. In a read() function, the value of zero indicates end-of-file.
2. If you do not call write() function in a loop then the entire
data would not be written.
Practice: 8.2 (Contd.)
Slide 14 of 22Ver. 1.0
Programming in C
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.
Error Handling
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.
Slide 15 of 22Ver. 1.0
Programming in C
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.
Error Handling (Contd.)
Slide 16 of 22Ver. 1.0
Programming in C
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.
Error Handling (Contd.)
Slide 17 of 22Ver. 1.0
Programming in C
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.
Using Random Access Seek in Files
Slide 18 of 22Ver. 1.0
Programming in C
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);
The lseek() Function
Slide 19 of 22Ver. 1.0
Programming in C
The following table lists the various values of the third
parameter (origin).
The lseek() Function (Contd.)
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.
Slide 20 of 22Ver. 1.0
Programming in C
The following table lists some instructions for moving to the
end or beginning of a file.
The lseek() Function (Contd.)
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);
Slide 21 of 22Ver. 1.0
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.
Slide 22 of 22Ver. 1.0
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.

More Related Content

What's hot

intro unix/linux 05
intro unix/linux 05intro unix/linux 05
intro unix/linux 05
duquoi
 
Embedded c program and programming structure for beginners
Embedded c program and programming structure for beginnersEmbedded c program and programming structure for beginners
Embedded c program and programming structure for beginners
Kamesh Mtec
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
Haresh Jaiswal
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
FISL XIV - The ELF File Format and the Linux Loader
FISL XIV - The ELF File Format and the Linux LoaderFISL XIV - The ELF File Format and the Linux Loader
FISL XIV - The ELF File Format and the Linux Loader
John Tortugo
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
gourav kottawar
 
Loader
LoaderLoader
Loader
nikhilshrama
 
intro unix/linux 06
intro unix/linux 06intro unix/linux 06
intro unix/linux 06
duquoi
 
Handout#11
Handout#11Handout#11
Handout#11
Sunita Milind Dol
 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Varun Mahajan
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
Satyamevjayte Haxor
 
Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
 
Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2Khaja Dileef
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
Taha Malampatti
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
Examinable Question and answer system programming
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programming
Makerere university
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
Satyamevjayte Haxor
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
duquoi
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
Docent Education
 

What's hot (20)

intro unix/linux 05
intro unix/linux 05intro unix/linux 05
intro unix/linux 05
 
ELF
ELFELF
ELF
 
Embedded c program and programming structure for beginners
Embedded c program and programming structure for beginnersEmbedded c program and programming structure for beginners
Embedded c program and programming structure for beginners
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
FISL XIV - The ELF File Format and the Linux Loader
FISL XIV - The ELF File Format and the Linux LoaderFISL XIV - The ELF File Format and the Linux Loader
FISL XIV - The ELF File Format and the Linux Loader
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
Loader
LoaderLoader
Loader
 
intro unix/linux 06
intro unix/linux 06intro unix/linux 06
intro unix/linux 06
 
Handout#11
Handout#11Handout#11
Handout#11
 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Examinable Question and answer system programming
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programming
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 

Viewers also liked

Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
List Of Excel Shortcuts
List Of Excel ShortcutsList Of Excel Shortcuts
List Of Excel ShortcutsPabolu Pavan
 
Ppt shortcut keys
Ppt shortcut keysPpt shortcut keys
Ppt shortcut keys
Rakesh Kumar Pandey
 
Excel shortcut key
Excel shortcut keyExcel shortcut key
Excel shortcut keyAtif Hossain
 
Microsoft Word Shortcut Keys
Microsoft Word Shortcut KeysMicrosoft Word Shortcut Keys
Microsoft Word Shortcut KeysvirtualMaryam
 
Microsoft excel shortcut keys
Microsoft excel shortcut keysMicrosoft excel shortcut keys
Microsoft excel shortcut keysskftboy
 

Viewers also liked (6)

Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
List Of Excel Shortcuts
List Of Excel ShortcutsList Of Excel Shortcuts
List Of Excel Shortcuts
 
Ppt shortcut keys
Ppt shortcut keysPpt shortcut keys
Ppt shortcut keys
 
Excel shortcut key
Excel shortcut keyExcel shortcut key
Excel shortcut key
 
Microsoft Word Shortcut Keys
Microsoft Word Shortcut KeysMicrosoft Word Shortcut Keys
Microsoft Word Shortcut Keys
 
Microsoft excel shortcut keys
Microsoft excel shortcut keysMicrosoft excel shortcut keys
Microsoft excel shortcut keys
 

Similar to C programming session 14

C programming session 14
C programming session 14C programming session 14
C programming session 14AjayBahoriya
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
Build process in ST Visual Develop
Build process in ST Visual DevelopBuild process in ST Visual Develop
Build process in ST Visual Develop
Gourav Kumar
 
C programming session 11
C programming session 11C programming session 11
C programming session 11AjayBahoriya
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
Mohamed Abdallah
 
Linux Systems Programming: File Handling
Linux Systems Programming: File HandlingLinux Systems Programming: File Handling
Linux Systems Programming: File Handling
RashidFaridChishti
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
farishah
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptx
sangeetaSS
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
Tanmay Baranwal
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
PragatiSutar4
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2hassaanciit
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
Gaurav Singh
 
File Handling
File HandlingFile Handling
File Handling
TusharBatra27
 
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
 
C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse students
Abdur Rahim
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
Sunil Patel
 
Built in function
Built in functionBuilt in function
Built in function
MD. Rayhanul Islam Sayket
 
Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008
guestd9065
 

Similar to C programming session 14 (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
Build process in ST Visual Develop
Build process in ST Visual DevelopBuild process in ST Visual Develop
Build process in ST Visual Develop
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Linux Systems Programming: File Handling
Linux Systems Programming: File HandlingLinux Systems Programming: File Handling
Linux Systems Programming: File Handling
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptx
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
File Handling
File HandlingFile Handling
File Handling
 
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
 
C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse students
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
Built in function
Built in functionBuilt in function
Built in function
 
Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008
 

More from Vivek Singh

C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clauseVivek Singh
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operatorsVivek Singh
 

More from Vivek Singh (20)

C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clause
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operators
 

Recently uploaded

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 

C programming session 14

  • 1. Slide 1 of 22Ver. 1.0 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
  • 2. Slide 2 of 22Ver. 1.0 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
  • 3. Slide 3 of 22Ver. 1.0 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.
  • 4. Slide 4 of 22Ver. 1.0 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.
  • 5. Slide 5 of 22Ver. 1.0 Programming in C Practice: 8.1 (Contd.) Solution: A file descriptor is a non-negative integer.
  • 6. Slide 6 of 22Ver. 1.0 Programming in C 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. Uses of Low-Level Input/Output
  • 7. Slide 7 of 22Ver. 1.0 Programming in C 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() Working with Low-Level Input/Output Functions
  • 8. Slide 8 of 22Ver. 1.0 Programming in C 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 ); The open() Function
  • 9. Slide 9 of 22Ver. 1.0 Programming in C 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); The close() Function
  • 10. Slide 10 of 22Ver. 1.0 Programming in C 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); The read() function
  • 11. Slide 11 of 22Ver. 1.0 Programming in C The write() function: Enables a user to write contents to a file. Has the following syntax: int write (int filedes, char *buffer, int size); The write() function
  • 12. Slide 12 of 22Ver. 1.0 Programming in C Practice: 8.2 1. Which of the following statements is true? a. At end-of-file, if a function is called repeatedly, it will give error. b. In a read() function, the value of zero indicates end-of-file. 2. What will happen if you do not call the write() function in a loop?
  • 13. Slide 13 of 22Ver. 1.0 Programming in C Solution: 1. In a read() function, the value of zero indicates end-of-file. 2. If you do not call write() function in a loop then the entire data would not be written. Practice: 8.2 (Contd.)
  • 14. Slide 14 of 22Ver. 1.0 Programming in C 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. Error Handling 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.
  • 15. Slide 15 of 22Ver. 1.0 Programming in C 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. Error Handling (Contd.)
  • 16. Slide 16 of 22Ver. 1.0 Programming in C 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. Error Handling (Contd.)
  • 17. Slide 17 of 22Ver. 1.0 Programming in C 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. Using Random Access Seek in Files
  • 18. Slide 18 of 22Ver. 1.0 Programming in C 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); The lseek() Function
  • 19. Slide 19 of 22Ver. 1.0 Programming in C The following table lists the various values of the third parameter (origin). The lseek() Function (Contd.) 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.
  • 20. Slide 20 of 22Ver. 1.0 Programming in C The following table lists some instructions for moving to the end or beginning of a file. The lseek() Function (Contd.) 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);
  • 21. Slide 21 of 22Ver. 1.0 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.
  • 22. Slide 22 of 22Ver. 1.0 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.

Editor's Notes

  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.