1. Which function is used to open a file in C?
Answer:
Explanation:
The fopen() function is used to open a file in C. It requires the file name and the mode (like 'r' for read, 'w' for write) as arguments.
2. What does the 'r+' mode do when opening a file with fopen() in C?
Answer:
Explanation:
The 'r+' mode opens a file for both reading and writing. The file must exist before opening it in this mode.
3. What is the return type of the fopen() function in C?
Answer:
Explanation:
fopen() returns a pointer to a FILE object. If the file cannot be opened, it returns NULL.
4. Which function is used to close a file in C?
Answer:
Explanation:
The fclose() function is used to close an open file. It takes a pointer to a FILE object as its argument.
5. What is the purpose of the fprintf() function in C?
Answer:
Explanation:
fprintf() is used to write formatted data to a file. It is similar to printf() but outputs to a file instead of the console.
6. How can you read a single character from a file in C?
Answer:
Explanation:
The getc() function reads a single character from a specified file and returns it. It takes a pointer to a FILE object as its argument.
7. What is the function feof() used for in C?
Answer:
Explanation:
The feof() function tests the end-of-file indicator for the given file stream and returns a non-zero value if the end of the file is reached.
8. Which of the following correctly reads a string from a file in C?
Answer:
Explanation:
fscanf() is used to read formatted input from a file. It works similarly to scanf() but reads from a file instead of standard input.
9. What is the output of the following C code if the file "data.txt" contains the text "Hello"?
FILE *file = fopen("data.txt", "r");
char str[10];
fscanf(file, "%s", str);
printf("%s", str);
fclose(file);
Answer:
Explanation:
The code successfully opens "data.txt", reads the string "Hello" into str, prints it, and then closes the file.
10. How do you write a single character to a file in C?
Answer:
Explanation:
The fputc() function writes a single character to the specified file. It takes the character and a FILE pointer as arguments.
11. Which function is used to read a block of data from a file in C?
Answer:
Explanation:
fread() is used for reading a block of data from a file. It requires pointers to a buffer, the size of each element, the number of elements, and a FILE pointer.
12. What is the purpose of fseek() function in C?
Answer:
Explanation:
fseek() is used to move the file pointer to a specific location in a file, defined by a given offset and a reference position.
13. What does the 'w' mode do when opening a file with fopen() in C?
Answer:
Explanation:
The 'w' mode opens a file for writing. If the file exists, it is truncated to zero length; otherwise, a new file is created.
14. How do you check for errors when opening a file in C?
Answer:
Explanation:
When opening a file, you should check if the returned FILE pointer is NULL, which indicates an error occurred during file opening.
15. What is the return value of the fwrite() function in C?
Answer:
Explanation:
fwrite() returns the number of elements successfully written to the file, which may be less than the number requested if an error occurs.
16. What is the purpose of the fflush() function in C?
Answer:
Explanation:
fflush() is used to flush the output buffer of a file stream, ensuring all buffered data is written to the file.
17. How can you append data to an existing file in C?
Answer:
Explanation:
The 'a' mode in fopen() opens a file for writing at the end of the file without truncating it, allowing data to be appended.
18. What happens if you try to write to a file opened in read-only mode in C?
Answer:
Explanation:
Attempting to write to a file opened in read-only mode will not succeed, and the operation will be ignored without changing the file.
19. What is the return type of the getc() function in C?
Answer:
Explanation:
The getc() function returns the character read as an unsigned char cast to an int or EOF on end of file or error.
20. Which function in C can be used to rename a file?
Answer:
Explanation:
The rename() function is used to change the name of a file. It takes two arguments: the current filename and the new filename.
Comments
Post a Comment
Leave Comment