• Tutorials
  • Courses
  • Tracks

File Handling

Last Updated :
Discuss
Comments

Question 1

Which header file is used for file operations in C++?

  • <iostream>

  • <fstream>

  • <file>

  • <stdio.h>

Question 2

 What does the following code do?

C++
ofstream file("data.txt", ios::app);
file << "Hello World\n";
file.close();


  • Creates a new file and writes data.

  • Overwrites the file contents.

  • Appends data to the file without deleting existing content.

  • Reads data from the file.

Question 3

How do you read content from a file in C++?

  • Use cin >>

  • Use ifstream and getline

  • Use printf()

  • Use ofstream

Question 4

What will the following code output?

C++
ifstream fin("input.txt");
string word;
fin >> word;
cout << word;
fin.close();


  •  Prints the first character from the file


  • Prints the entire file

  • Prints the first word from the file

  • Compilation error


Question 5

What is the purpose of remove("file.txt")?

  • Rename the file

  • Delete the file

  • Close the file

  • Open the file

Question 6

 Which of the following opens a file for both reading and writing?


  •  ifstream

  • ofstream

  • fstream

  • cin

There are 6 questions to complete.

Take a part in the ongoing discussion