Lab P18 β File handling.
Aim
Aim: Demonstrate basic file handling.
Theory
Demonstrate basic file handling.
C++ Program
#include
#include
using namespace std;
int main(){
ofstream out("data.txt"); out<<"Hello File\n"; out.close();
ifstream in("data.txt"); string line; getline(in,line); cout<<"Read: "< Expected Output
Read: Hello FileExplanation
- ofstream writes; ifstream reads.
- close() releases file.
Viva / Important Questions
Short Questions
- Which header for file I/O?
- Difference ofstream/ifstream?
Long Questions
- Explain the program in detail.
- Discuss key concepts demonstrated.
MCQ Practice
1. File write uses:
- ofstream
- ifstream
- cin
- cout
Answer: ofstream.
2. File read uses:
- ifstream
- ofstream
- cin
- cout
Answer: ifstream.
3. Header for files:
- fstream
- iostream
- stdio.h
- None
Answer: fstream.
4. close() does:
- Closes file
- Deletes file
- Opens file
- Nothing
Answer: Closes file.
5. getline reads:
- A line
- A char
- A number
- Nothing
Answer: A line.
Quick Revision
- ofstream write, ifstream read.
- Always close files.