file handling interview questions in C.File handling frequently Asked questions in C

Q6. What is the purpose of the fseek() function in file handling?
The fseek() function changes the current position of a file attached to a stream to a new location in the file. The next operation in the stream occurs at a new location. On a stream opened for update, the next operation may be a read or write operation


Q7.How do you check if a file exists in C?
Use the fopen() function to check if a file exists by trying to read it. Use the stat() function to check if the file exists by trying to read properties from the file.


Q8.What is the difference between feof() and ferror() functions?
feof returns non-zero when the end of the file is read into the named input stream, zero otherwise. ferror returns nonzero if an error occurred while reading or writing the named stream, zero otherwise.


Q9.How do you handle errors while working with files in C?
The C language provides perror() and strerror() functions that can be used to display the text message associated with errno.


Q10.How do you rename a file in C?

The rename() function renames the file specified by the old name to the given name by the new name. A pointer to oldname must specify the name of an existing file. The newname pointer essentialy not require the name of an existing file. You can not rename a file with an current file name.


Q11.How do you delete a file in C?
The remove() function is used in C to delete a file. It is available in the standard I/O <stdio.h> header file.


Q12.What is the purpose of the rewind() function in file handling?
In C, the rewind() function returns the file position pointer to the beginning of the file. It takes a file pointer as input and moves the pointer to the beginning of the file, allowing data to be read from the beginning again.


Q13.How do you copy the contents of one file to another in C?
1.Input file path of source and destination file.
2.Open source file in read ( r ) and destination file in write (w) mode.
3.Read character from source file and write it to destination file using fputc().
4.Repeat step 3 till source file has reached end.


Q14.How do you append data to a file in C?
ToThe following function is used to append the content in a file.
fopen("my_file.txt", "a") opens the file "my_file.txt" in append mode.


Q15.How do you read and write binary files in C?Binary files take up less space and are easy to move from one storage location to another. Read, write, and seek operations can be performed on binary files using the fread(), fwrite(), and fseek() functions.


16.How do you handle end-of-file conditions in C file handling?
To solve this problem, C offers the feof() function. The feof() function returns a non-zero value simply when the end of the file is reached, representing that there are no more characters to read. Contrariwise, it returns 0 if the end of the file has not been reached.


Q17.How do you set file permissions in C?
To change the permissions of an existing file, call chmod. This function uses the stated permission bits and ignores the file creation mask. In normal use, the file creation mask is initialized by the user's login shell using the umask shell command and inherited by all sub-processes.

Q18.How do you check the size of a file in C?
In C language we can use the fseek() and ftell() functions to decide file size. The fseek() function allows you to set a file position indicator that determines the location in the file where the next operation will occur. In this case, move the pointer to the end of the file and check the file size.


Q19.How do you create a temporary file in C?
The C library function FILE *tmpfile(void) creates a temporary file in binary update mode (wb+). The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates.


Q20.How do you free memory allocated using pointers in C?
The opendir function opens and returns a directory stream for reading a directory whose file name is directory_name.
Stream type - DIR*. If it fails, opendir returns a null pointer.


Previous Topic:-->>Pointers FAQ in C. || Next topic:-->>Storage class FAQ in C.