Sei sulla pagina 1di 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.

NIIT

SEM Q/CPR/CR/SESSION 8/1/VER07/2003

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.

NIIT

SEM Q/CPR/CR/SESSION 8/2/VER07/2003

Review Quiz
#4 Match the following flag values used in open() function with their description.
Flag Values
O_RDONLY

Description
Creates a new file if the required file does not exist

O_WRONLY
O_RDWR O_CREAT O_APPEND O_TRUNC

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

NIIT

SEM Q/CPR/CR/SESSION 8/3/VER07/2003

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

NIIT

SEM Q/CPR/CR/SESSION 8/4/VER07/2003

SOLUTIONS TO REVIEW QUIZ (Contd.


#4
Flag Values
O_RDONLY

Description
Opens a file for performing only read opearations

O_WRONLY
O_RDWR O_CREAT O_APPEND O_TRUNC

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

NIIT

SEM Q/CPR/CR/SESSION 8/5/VER07/2003

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

NIIT

SEM Q/CPR/CR/SESSION 8/6/VER07/2003

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

NIIT

SEM Q/CPR/CR/SESSION 8/7/VER07/2003

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.

NIIT

SEM Q/CPR/CR/SESSION 8/8/VER07/2003

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 );

NIIT

SEM Q/CPR/CR/SESSION 8/9/VER07/2003

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);

NIIT

SEM Q/CPR/CR/SESSION 8/10/VER07/2003

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);

NIIT

SEM Q/CPR/CR/SESSION 8/11/VER07/2003

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.

NIIT

SEM Q/CPR/CR/SESSION 8/12/VER07/2003

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.

NIIT

SEM Q/CPR/CR/SESSION 8/13/VER07/2003

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.

NIIT

SEM Q/CPR/CR/SESSION 8/14/VER07/2003

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

NIIT

SEM Q/CPR/CR/SESSION 8/15/VER07/2003

Error Handling (Contd.)

errno values
ENOSPC EIO EBADF

Description
Specifies that the disc is out of space and the file can not be created Specifies that there was a hardware error Specifies that the file descriptor passed to read, write, or close the file is invalid.

NIIT

SEM Q/CPR/CR/SESSION 8/16/VER07/2003

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.

EISDIR

ENOENT
EMFILE EROFS

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..
SEM Q/CPR/CR/SESSION 8/17/VER07/2003

NIIT

Error Handling (Contd.)


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

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.

NIIT

SEM Q/CPR/CR/SESSION 8/18/VER07/2003

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() {

NIIT

SEM Q/CPR/CR/SESSION 8/19/VER07/2003

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) {
NIIT SEM Q/CPR/CR/SESSION 8/20/VER07/2003

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;

}
NIIT SEM Q/CPR/CR/SESSION 8/21/VER07/2003

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() {

NIIT

SEM Q/CPR/CR/SESSION 8/22/VER07/2003

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); }

NIIT

SEM Q/CPR/CR/SESSION 8/23/VER07/2003

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); }

NIIT

SEM Q/CPR/CR/SESSION 8/24/VER07/2003

SOLUTION TO CLASSROOM EXERCISE (Contd.)


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

NIIT

SEM Q/CPR/CR/SESSION 8/25/VER07/2003

Potrebbero piacerti anche