Important Introduction to Programming Short Questions
Chapter: 19
Question 1:
How can you read a file in reverse order?
Answer:
To read a file in reverse, position the file pointer at the end and move backward byte by byte using seekg() and read in reverse order.
Chapter: 19
Question 2:
What is the benefit of using read() and write() functions for file operations?
Answer:
They process data in chunks, which is more efficient than handling one byte at a time, reducing the number of I/O operations.
Chapter: 19
Question 3:
What should you consider when using read() and write() functions?
Answer:
Ensure there is enough memory to hold the data and avoid using magic numbers by employing sizeof() to handle variable sizes.
Chapter: 19
Question 4:
How does using sizeof() benefit file operations?
Answer:
Using sizeof() ensures that the correct number of bytes is written or read for different data types, making the code portable and independent of the internal representation of data.
Chapter: 19
Question 5:
How do binary files differ from text files in terms of visibility?
Answer:
Binary files may not be properly viewed in text editors, as they contain raw data in binary format rather than human-readable text.
Chapter: 19
Question 6:
What does the tellg() function do?
Answer:
The tellg() function returns the current position of the file pointer in an input file stream.
Chapter: 19
Question 7:
How do you copy a file using read() and write() functions?
Answer:
Use a loop to read chunks of data into a buffer and then write the buffer to the output file, improving efficiency compared to single-byte operations.
Chapter: 19
Question 8:
What is the purpose of the tellp() function?
Answer:
The tellp() function returns the current position of the file pointer in an output file stream.
Chapter: 19
Question 9:
What happens if you move the file pointer beyond the end or before the start of a file?
Answer:
Moving the pointer beyond the file's limits can lead to errors or undefined behavior, such as failing to read or write data correctly.
Chapter: 20
Question 10:
What is a structure in C/C++?
Answer:
A structure is a collection of variables, possibly of different types, grouped under a single name. It allows related data to be stored together.