C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
121.What are the different errors during file I/O operations? How are errors handled during different file operations?
Only few, out of many, errors during file I/O operations are listed below:
•No such file or directory
•Permission denied
•Bad file number
•File already exists
•Too many open files
•Path not found
•Not enough memory
Following assumptions are made during I/O operations:
•File that we are trying to work with exists.
•There is enough space on the disk.
•The file handling functions such as fseek, fread, fwrite, carry out requested task. The following methods can be used to handle different types of error during file operations.
•Check for NULL pointer: the function fopen returns NULL pointer if it cannot open the file. So if the file pointer contains the value NULL then there is some kind of error. It can be done as:
fptr = fopen(“demo.txt”,”r”); if(fptr == NULL)
{
printf(“Error Occurred”); exit(-1);
}
57
•Using ferror() macro: it tests the given file for a read or write error. It returns anon-zero value if an error is detected on the file. It is used by passing a file pointer as its argument.
if(ferror(file_pointer)!=0)
{
print(“Error Detected”);
}
The type of error that has occurred can be display using following methods:
•Using global variable errno and function strerror(): whenever an error occurs in a system call, errno is set to a value indicating the type of error. The corresponding type of error as represented by errno can be displayed using strerror() function.
if(fp == NULL)
{
printf(“Error : %s”, stderror(errno)); exit(-1);
}
•Using perror() function: this function displays the string passed to it, followed by a colon, a space, and then the string representing the type of error.
if(fp == NULL)
{
perror(“Error”); exit(-1);
}
122.Explain about different types of functions that are generally used.
The various functions along with their descriptions are listed below:
•fopen(“String1”,”String2”) :
•String1 is a name of a file which we are trying to open. If only the file name is given then the default path will be c:tcbin. The path of the file can be used as d:\myfolder\demo.txt.
58
•String2 is the mode in which the file has to be opened. r for reading, w for writing, a for appending. r+ , w+ and a+ can be used for reading and writing at the same time when the file is opened.
•If the function fopen() finds the intended file or is able to create new file as required then it will return the file pointer. On failure it returns the NULL value.
Eg. FILE *fp;
fp = fopen(“demo.txt”,”w”); – it will open/create demo.txt in writing mode.
•fclose(file_pointer): After all the processing is done on the file, it must be closed using a
function fclose(). Eg : fclose(fp);
•putc(arg1,arg2): arg1 can be either character constant or character variable, arg2 is the file pointer which points to the file where the character is to be written.
Eg: putc(c, fp) – writes the content of variable c on file pointed by fp.
•getc(arg1) : arg1 is the file pointer, this function returns a character from a file pointed by the file pointer. Hence the function is generally assigned to a character variable. Eg: c=getc(fp); – here c is a character variable and it stores the value returned by the function getc() which is the character stored in file pointed by fp.
•fprintf(file_pointer,”control string”,arg1,arg2,…argn) : This function is similar to that of printf but file pointer must be added as an argument which points to the file in which the data are to be written. It is used to write data on to the file.
Eg : fprintf(fp,” %s %d”,name,age); – it will write the content of variable name and
age on the file pointed by fp.
•fscanf(file_pointer,”control string”,arg1,arg2,arg3…argn) : This function reads the data from a file. The file pointer must be used as an argument which points to the file from where data is to be read.
Eg: fscanf(fp,”%s %d”, name, &age); – it will read a string and a integer from the file pointed by fp and stores the data on the variable name and age respectively.
•fwrite(address of data block, size of block, number of blocks, file pointer) : This function is used to write the block of data on to the file. It takes four arguments the first argument is a constant pointer which is the address of the block which is to be written on to the file, second argument is the size of the data block to be written on the file pointed by the file pointer. Third argument represents the number of such blocks to be written on the file, and finally the last argument is the file pointer pointing to the file where data is to written. Eg :
59
fwrite(&s, sizeof(s), 1,fp); – Here s is the structure variable which may consist of several members. The content of structure (s) is written on the file pointed by fp.
•fread(address of data block, size of block, number of blocks, file pointer) : It is used to read
a block of data from a file. Eg: fread(&s, sizeof(s), 1, fp); Here the function reads the block of data from the file and stores the content on the structure variable s;
•feof(file_pointer) : This function is used to detect the end of file condition. This function returns a non-zero value (true) if an end of file condition has been detected and a value zero (false) if an end of file is not detected.
•rewind(file_pointer) : This function is used to move the file pointer to the starting position of the file(buffer). It is generally used when the file is opened in writing and reading (multiple modes) mode. Such that after writing into the file the pointer will be at the last so we need to rewind the file pointer to the starting positions.
•fseek(FILE_pointer, offset, whence) : The function fseek() is used to access the file randomly. This function can be used to position the file pointer as required. The second argument is the number of bytes the pointer to be moved from a particular position specified by the third argument. The third argument specifies from where the position of the pointer is to be repositioned. The third argument can be:
•SEEK_END (2) – with reference to the end of file.
•SEEK_CUR (1) – with reference to the current position of the pointer on the file.
•SEEK_SET (0) – with reference to the starting position of the file.
Eg: fseek(fp, 10, SEEK_SET); It will move the file pointer by 10 bytes with respect to the starting point. If we read a character from a file then the 11th character will be read.
Eg: fseek(fp, -20, SEEK_END): It will move the file pointer 20 bytes backwards from the end of the file.
123.Write the difference between
•Writing mode and Appending Mode
|
SN
|
Writing Mode
|
Appending Mode
|
|
1.
|
File Pointer points at the beginning
|
File Pointer points at the end of the file
|
|
2.
|
Content of file is overwritten
|
Content is added at the end
|
60
•Text file and binary file
|
SN
|
Text File
|
Binary File
|
|
1.
|
New line character is converted into
|
The new line character does not require
|
|
carriage return and line feed combination
|
any conversion.
|
|
|
before writing on the disk. And again the
|
||
|
combination is converted back to newline
|
||
|
when file is read by C program.
|
||
|
2.
|
EOF (End of file) character to represent
|
No EOF, programmer must keep track of
|
|
the terminating point of a file
|
data being accessed.
|
3.Values are stored as set of characters. Same number of bytes on disks as the
Causing 35221 to occupy 5 bytes rather values occupy in memory. than 2 bytes.
61
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
