Programming Language & Its Applications β Virtual Functions and File Handling, NEC licence examination syllabus (Nepal Engineering Council).
C++ streams replace C's FILE* β and because they are objects, the destructor closes the file for you.
fopen and std::ifstream is RAII, and it eliminates a real bug class. A C file handle leaks if you return early or throw; a C++ stream cannot, because its destructor runs on every exit path. This is the same reason Python's with open(...) exists and why Java added try-with-resources in 2011 β three languages independently concluding that "remember to close it" is a losing strategy. Search "resource leak file descriptor exhaustion" to see what happens when a long-running server gets this wrong: it eventually fails with "Too many open files" and stops serving anyone.#include <iostream>
#include <fstream>
#include <string>
int main() {
/* WRITE β ofstream truncates by default */
{
std::ofstream out("marks.txt");
if (!out) { std::cerr << "cannot open for writing\n";
return 1; }
out << "101 Ram 87.5\n"
<< "102 Sita 91.0\n"
<< "103 Hari 76.5\n";
} /* destructor closes and flushes HERE */
/* APPEND β does not destroy what is there */
{
std::ofstream out("marks.txt", std::ios::app);
out << "104 Gita 68.0\n";
}
/* READ */
{
std::ifstream in("marks.txt");
if (!in) { std::cerr << "cannot open for reading\n";
return 1; }
std::string line;
int n = 0;
while (std::getline(in, line))
std::cout << " " << ++n << " | " << line << "\n";
std::cout << " read " << n << " lines\n";
}
/* a file that does not exist */
std::ifstream missing("no_such_file.txt");
std::cout << " missing file: is_open=" << std::boolalpha
<< missing.is_open()
<< " fail=" << missing.fail() << "\n";
/* reopening the same object requires close() first */
std::ifstream f("marks.txt");
f.close();
f.open("marks.txt");
std::cout << " reopened: " << f.is_open() << "\n";
return 0;
}
Output:
1 | 101 Ram 87.5
2 | 102 Sita 91.0
3 | 103 Hari 76.5
4 | 104 Gita 68.0
read 4 lines
missing file: is_open=false fail=true
reopened: true
fopen returns NULL, which crashes loudly the moment you use it β annoying but obvious. A failed C++ stream is a valid object that quietly does nothing, so the bug surfaces far from its cause. Check every open.
#include <iostream>
#include <fstream>
#include <string>
/* returns bytes copied, or -1 on error */
long copyFile(const std::string &src, const std::string &dst) {
std::ifstream in(src, std::ios::binary);
if (!in) {
std::cerr << " cannot read " << src << "\n";
return -1;
}
std::ofstream out(dst, std::ios::binary);
if (!out) {
std::cerr << " cannot write " << dst << "\n";
return -1;
}
/* stream-to-stream copy: one line, buffered */
out << in.rdbuf();
/* verify the WRITE succeeded β a full disk fails here,
not at open time */
out.flush();
if (!out) { std::cerr << " write failed\n"; return -1; }
in.clear();
in.seekg(0, std::ios::end);
return static_cast<long>(in.tellg());
}
int main() {
/* make a source file */
{ std::ofstream f("source.txt");
f << "Nepal Engineering Council\n2026 licence exam\n"; }
long n = copyFile("source.txt", "target.txt");
std::cout << " copied " << n << " bytes\n";
{ std::ifstream f("target.txt");
std::string line;
while (std::getline(f, line))
std::cout << " > " << line << "\n"; }
/* failure path */
if (copyFile("does_not_exist.txt", "x.txt") < 0)
std::cout << " handled the error cleanly\n";
return 0;
}
Output:
copied 44 bytes
> Nepal Engineering Council
> 2026 licence exam
cannot read does_not_exist.txt
handled the error cleanly
<filesystem>, which finally gave the language portable file operations β fs::exists(), fs::file_size(), fs::copy(), directory iteration, and path manipulation that handles Windows backslashes correctly. Before it, every project rolled its own or used Boost. Search "C++17 filesystem library tutorial"; fs::file_size("x.txt") replaces the whole seekg/tellg dance above with one call, and fs::copy replaces the function.ifstream, ofstream, fstream) and the header <fstream>. The open-mode flags table is asked directly β particularly that ofstream truncates by default and that app forces every write to the end. Always show the if (!file) check; marks are lost for omitting it. Know that the destructor closes the file (RAII) and that close() is needed only to reopen. Distinguish app from ate.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loadingβ¦