December 2018
Intermediate to advanced
552 pages
12h 18m
English
Opening a file in C++ is as simple as providing a std::fstream object with the filename and path of the object you wish to open. This is shown as follows:
#include <fstream>#include <iostream>int main(){ if (auto file = std::fstream("test.txt")) { std::cout << "success\n"; } else { std::cout << "failure\n"; }}// > g++ -std=c++17 scratchpad.cpp; touch test.txt; ./a.out// success
In this example, we open a file named test.txt, which we previously created using the POSIX touch command. This file is opened with read/write permissions (as that is the default mode).
The file is stored in a variable named file, and it is checked to ensure it was properly opened using the bool operator overload that std::fstream provides. ...
Read now
Unlock full access