Save
...
Done [Finals]
Computer Programming 2
File Handling
Save
Share
Learn
Content
Leaderboard
Share
Learn
Created by
Marc
Visit profile
Cards (16)
What does C++ file handling refer to?
Creating files and performing
read/write
operations
View source
Which header file must be included for file handling in C++?
#include <
fstream
>
View source
What is the purpose of the ofstream class?
To create or open and write to a file
View source
What is the purpose of the ifstream class?
To read a file
View source
How do you create or open a text file in C++?
Using
ofstream writeFile("File1.txt");
View source
What is the correct way to write to a file in C++?
writeFile << "Text";
View source
What does writeFile.close() do?
It
closes
the file after writing
View source
How can you check if a file is open in C++?
Using writeFile.is_open()
View source
What is the purpose of getline() in file reading?
To read a line
from the file
View source
How do you display the content of a file in C++?
Using
cout
<<
text
;
View source
What does the ios::out flag do?
Creates or opens a file for writing
View source
What does the ios::in flag do?
Reads a file
View source
What does the ios::app flag do?
Appends new content to a file
View source
What are the steps to create, write, and close a file in C++?
Include necessary headers
: <code>#include <iostream></code> and <code>#include <fstream></code>
Create an ofstream object
: <code>ofstream writeFile("File1.txt");</code>
Write to the file
: <code>writeFile << "Text";</code>
Close the file
: <code>writeFile.close();</code>
View source
What are the steps to read and display the content of a file in C++?
Include necessary headers
: <code>#include <iostream></code> and <code>#include <fstream></code>
Create an ifstream object
: <code>ifstream readFile("File1.txt");</code>
Check if the file is open
: <code>if(readFile.is_open())</code>
Use getline to read lines
: <code>while (getline(readFile, text))</code>
Display the content
: <code>cout << text;</code>
Close the file
: <code>readFile.close();</code>
View source
What is an alternative way to handle files using fstream?
Use <code>
fstream
</code> for both reading and writing
Flags:
<code>
ios::out
</code> for writing
<code>
ios::in
</code> for reading
<code>
ios::app
</code> for appending
View source