0% found this document useful (0 votes)
52 views25 pages

CPB2 XP 08

A file descriptor is a name, which is given to a file on creation. It is a non-negative integer that is returned by open() function on opening a file. A programmer wants to open a file named exam.txt. However, on executing the program the programmer encountered certain errors.

Uploaded by

DeependraMishra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views25 pages

CPB2 XP 08

A file descriptor is a name, which is given to a file on creation. It is a non-negative integer that is returned by open() function on opening a file. A programmer wants to open a file named exam.txt. However, on executing the program the programmer encountered certain errors.

Uploaded by

DeependraMishra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Review Quiz

#1 Which one of the following statements best describes a file descriptor? a. A file descriptor is a name, which is given to a file on creation. b. A file descriptor is a non-negative integer that is returned by open() function on opening a file. c. A file descriptor is the name of a file, which is used for writing on the file.

5/4/12

Review Quiz (contd.)


#2 A programmer wants to open a file named exam.txt. For this purpose, he wrote the following line of code. However, on executing the program the programmer encountered certain errors. Find the errors in the code? filedescriptor = open (O_WRONLY | O_CREATE, exam.txt, 0); #3 State whether the following statement is true or false. The read operation starts from the current file position.

5/4/12

Review Quiz

#4 Match the following flag values used in open() function with their description.
Flag Values
O_RDONLY O_WRONLY O_RDWR O_CREAT O_APPEND O_TRUNC

Description
Creates a new file if the required file does not exist Truncates a file to the length of zero bytes Opens a file for performing only read opearations Opens a file for appending. Opens a file for performing only write operations Opens a file for performing both read and write operations

5/4/12

SOLUTIONS TO REVIEW QUIZ


#1 b #2 The sequence of the parameters is wrong in the function and the flag value O_CREATE has an extra E at the end. The correct line of code is: filedescriptor = open (exam.txt, O_WRONLY | O_CREAT, 0); #3 True

5/4/12

SOLUTIONS TO REVIEW QUIZ (Contd.


#4
Flag Values
O_RDONLY O_WRONLY O_RDWR O_CREAT O_APPEND O_TRUNC

Description
Opens a file for performing only read opearations Opens a file for performing only write operations Opens a file for performing both read and write operations Creates a new file if the required file does not exist Opens a file for appending Truncates a file to the length of zero bytes

5/4/12

Low Level Input/Output


Objectives At the end of this session, you will be able to : Define Low Level Input/Output Perform Low Level File Handling Functions using: open() function close() function read() function write() function Understand Random Access in files Access file contents using the lseek() function Identify errors using errno

5/4/12

Low Level Input/Output


In C, you can access files and devices using two groups of functions, Higher Level I/O or Stream Level I/O and Low Level I/O Low Level I/O functions provide direct access to files and devices, such as tape drives, disk drives, and serial port In C, all peripheral devices are considered as files in the file system A file descriptor is used to identify a file

5/4/12

Low Level Input/Output (Contd.)


You can use Low Level I/O functions for the following purposes: Accessing files and devices directly Reading the binary files in large chunks Performing I/O operations quickly and efficiently as it works at a lower level of system. There are fewer interfaces to go through in between the function calls and the system.

5/4/12

Low Level Input/Output Functions


open() Function The open() function can be used to open an existing file or create a new file This function returns a file descriptor for the file name passed to it. The following example illustrates the use of the open() function: int open(char *filename, int flags, int perms );

5/4/12

Low Level Input/Output Functions (Contd.)


close() Function The close() function closes the file that was opened using the open() function It takes the file descriptor as a parameter to close the file If the file closes successfully it returns 0, otherwise -1 if there is an error The following example illustrates the use of the close () function : int close(int filedes);

5/4/12

Low Level Input/Output Functions (Contd.)


read() The C Low Level I/O system defines read() function that reads data from a file. The read operation starts from the current file position. The following example illustrates the use of the read(): int read (int filedes, char *buffer, int size);

5/4/12

Low Level Input/Output Functions (Contd.)


write() Function The write() function enables you to write contents to a file. The following example illustrates the use of the write(): int write (int filedes, char *buffer, int size); In case the file size is large, the write() function should always be called in an iterating loop, until all the data is written.

5/4/12

Random Access Seek


The read and write operations on files are usually sequential in nature. Random Access Seek 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.

5/4/12

Random Access Seek (Contd.)


The lseek() function returns the file position, as measured in bytes from the beginning of the file. The use of lseek() is illustrated in the following example: long lseek (int filedes, long offset, int origin); In this example: The first parameter is the file descriptor. The second parameter specifies the number of bytes to move the file position.

5/4/12

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
EACCES ENAMETOOLONG

Description
Specifies that the program has failed to access one of the directories in the file. Indicates that the file name is too long

5/4/12

Error Handling (Contd.)


errno values Description

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.

5/4/12

Error Handling (Contd.)


There are certain errno values that are specific to the open() function:

errno values
EEXIST

Description
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. Specifies that the file is actually a directory. Specifies that some of the file components do not exist. Specifies that too many files are open. 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..

EISDIR ENOENT EMFILE EROFS

5/4/12

Error Handling (Contd.)


There are certain errno values that are specific to the write() function:

errno values
EFBIG

Description
Specifies that the file will become too large if the data is written on it. Specifies that the write operation is temporarily interrupted. that the file is actually a directory.

EINTR

5/4/12

CLASSROOM EXERCISE
#1 Read a file after the first 10 characters and print it on screen.

SOLUTION TO CLASSROOM EXERCISE # 1 #include<sys/types.h> #include<sys/stat.h> #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #include<unistd.h> int main() {

5/4/12

CLASSROOM EXERCISE
int rdescriptor, i, readchar; char buffer [1024]; for(i=0;i<1024;i++) buffer[i] = '\0'; if((rdescriptor = open("example.txt",O_RDONLY,0))<0) printf("Open Failed\n"); lseek(rdescriptor,10,SEEK_SET); if((readchar = read(rdescriptor,buffer,1024))>0) { if(readchar>0) {
5/4/12

CLASSROOM EXERCISE
printf("\n The content of file after 10 bytes are \n"); printf("%s\n",buffer); } } else if(readchar==0) printf("Read Failed \n"); close(rdescriptor); return 0; }
5/4/12

CLASSROOM EXERCISE
# 2 Write your name 10 character after the end of the file. Also, use the exit() function to terminate the execution if you are unable to write or open the file. SOLUTION TO CLASSROOM EXERCISE # 2 #include<sys/types.h> #include<sys/stat.h> #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #include<unistd.h> int main() {

5/4/12

SOLUTION TO CLASSROOM EXERCISE (Contd.)


int wdescriptor; char name[8] = Ramesh; int namechar =8, writechar; if((wdescriptor=open(example.txt,O_WRONLY,0))<0) { printf(Open Failed.\n); exit(1); }

5/4/12

SOLUTION TO CLASSROOM EXERCISE (Contd.)


while(namechar>0) { lseek(wdescriptor,10,SEEK_END); if((writechar=write(wdescriptor,name,namechar))<0) { printf(Write Failed.\n); exit(1); }

5/4/12

SOLUTION TO CLASSROOM EXERCISE (Contd.


namechar - =writechar; } close(wdescriptor); return 0; }

5/4/12

You might also like