CPB2 XP 08
CPB2 XP 08
#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
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
5/4/12
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
5/4/12
5/4/12
5/4/12
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
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
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
5/4/12
5/4/12
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
ENOSPC
Specifies that the disc is out of space and the file can not be created
EIO
EBADF
Specifies that the file descriptor passed to read, write, or close the file is invalid.
5/4/12
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..
5/4/12
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
5/4/12
5/4/12
5/4/12