Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#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
#3 State whether the following statement is true or false. The read operation starts from the current file position.
NIIT
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
NIIT
#3 True
NIIT
Description
Opens a file for performing only read opearations
O_WRONLY
O_RDWR O_CREAT O_APPEND O_TRUNC
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
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
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
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
NIIT
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
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
CLASSROOM EXERCISE
int rdescriptor, i, readchar; char buffer [1024]; for(i=0;i<1024;i++) buffer[i] = '\0';
if(readchar>0) {
NIIT SEM Q/CPR/CR/SESSION 8/20/VER07/2003
CLASSROOM EXERCISE
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
NIIT
NIIT
NIIT