Skip to main content

Important Introduction to Programming Short Questions

Chapter: 18
Question 1:
Why is error checking important in file handling?
Answer: Error checking is important in file handling to ensure that operations like opening a file are successful, preventing issues like attempting to access a file that doesn't exist.
Chapter: 18
Question 2:
What does the code if (!myFile) check for in a C++ program?
Answer: It checks whether the file myFile was successfully opened. If not, an error message is shown, and the program exits.
Chapter: 18
Question 3:
What is the purpose of the exit(1) statement in the code?
Answer: The exit(1) statement terminates the program immediately, returning control to the operating system if an error occurs.
Chapter: 18
Question 4:
What file does the program read from and print in the provided example?
Answer: The program reads from a file named myfile.txt, which contains employee names, salaries, and departments.
Chapter: 18
Question 5:
How are the file streams opened for input in the given program?
Answer: The file stream is opened for input using the command inFile.open(inputFileName).
Chapter: 18
Question 6:
What happens if the input file fails to open in the provided program?
Answer: If the input file fails to open, an error message is displayed, and the program exits with exit(1).
Chapter: 18
Question 7:
What is the function of eof() in file handling?
Answer: The eof() function checks if the end of the file has been reached during reading.
Chapter: 18
Question 8:
How does the program read data from the file in the provided example?
Answer: The program reads data from the file using a while loop with inFile >> name >> sal >> dept;, printing the details to the screen.
Chapter: 18
Question 9:
What is the default behavior of the ios::out mode when opening a file?
Answer: The ios::out mode creates a new file if it doesn't exist, or truncates (clears) the file if it already exists.
Chapter: 18
Question 10:
How do you open a file in append mode in C++?
Answer: To open a file in append mode, use myFile.open("filename", ios::app);.