-
Notifications
You must be signed in to change notification settings - Fork 1
Cpp Usage
This page documents the C++ interface of cfstream, provided by cfs.hpp.
Use this page when you want to understand how the C++ version works in detail, including standard stream redirection, independent file streams, version query, closing behavior, and common usage rules.
Use the C++ header:
#include "cfs.hpp"The header provides all C++ features inside the cfs namespace.
cfs::cfs(...);
cfs::closecfs();
cfs::cppfs(...);
cfs::closecppfs();
cfs::cin;
cfs::cout;
cfs::getversion();cfs.hpp supports:
- C++98 or later
The header includes the required standard headers internally:
#include <fstream>
#include <cstdio>
#include <iostream>So in many simple programs, including cfs.hpp alone is enough.
All C++ interfaces are placed in the cfs namespace.
Example:
#include "cfs.hpp"
int main() {
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;
cfs::cout << n << '\n';
cfs::closecppfs();
return 0;
}You may also import selected names with using, but this is optional.
#include "cfs.hpp"
using cfs::cppfs;
using cfs::closecppfs;
using cfs::cin;
using cfs::cout;
int main() {
cppfs("test.in", "test.out");
int n;
cin >> n;
cout << n << '\n';
closecppfs();
return 0;
}Avoid using namespace cfs; in larger projects, because names such as cin and cout may become confusing when used together with std::cin and std::cout.
The C++ header provides two different file I/O modes:
| Mode | Functions | Main Streams Used | Purpose |
|---|---|---|---|
| Standard stream redirection |
cfs::cfs, cfs::closecfs
|
std::cin, std::cout, scanf, printf
|
Redirect standard input and output |
| Independent C++ file streams |
cfs::cppfs, cfs::closecppfs
|
cfs::cin, cfs::cout
|
Use separate file streams without changing standard streams |
These two modes are different and should not be confused.
void cfs::cfs(const char* input, const char* output);This function redirects standard input and/or standard output.
Internally, it uses freopen.
#include <iostream>
#include "cfs.hpp"
int main() {
cfs::cfs("test.in", "test.out");
int n;
std::cin >> n;
std::cout << n << '\n';
cfs::closecfs();
return 0;
}This program:
- reads from
test.in - writes to
test.out
After calling:
cfs::cfs("test.in", "test.out");the following streams/functions are affected:
std::cin
std::cout
scanf
printfThey will use the redirected standard input/output streams.
#include <iostream>
#include "cfs.hpp"
int main() {
cfs::cfs("test.in", NULL);
int n;
std::cin >> n;
std::cout << n << '\n';
cfs::closecfs();
return 0;
}Input is read from test.in, while output still goes to the console.
If you are using C++11 or later, you can also use nullptr:
cfs::cfs("test.in", nullptr);#include <iostream>
#include "cfs.hpp"
int main() {
cfs::cfs(NULL, "test.out");
int n;
std::cin >> n;
std::cout << n << '\n';
cfs::closecfs();
return 0;
}Input still comes from the console, while output is written to test.out.
If you are using C++11 or later, you can also use nullptr:
cfs::cfs(nullptr, "test.out");Both parameters can be NULL.
cfs::cfs(NULL, NULL);In C++11 or later:
cfs::cfs(nullptr, nullptr);This is allowed, but it has no practical effect.
void cfs::closecfs();This function attempts to restore standard input and output back to the console.
Example:
#include <iostream>
#include "cfs.hpp"
int main() {
cfs::cfs("test.in", "test.out");
int n;
std::cin >> n;
std::cout << n << '\n';
cfs::closecfs();
std::cout << "Back to console" << '\n';
return 0;
}Platform behavior:
| Platform | Console Device |
|---|---|
| Windows | CON |
| Linux / macOS / other Unix-like systems | /dev/tty |
closecfs() uses these device names internally to restore the console streams.
void cfs::cppfs(const char* input, const char* output);This function opens two independent C++ file streams:
cfs::cin
cfs::coutThey are separate from:
std::cin
std::cout#include <iostream>
#include "cfs.hpp"
int main() {
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;
cfs::cout << n << '\n';
std::cout << "This goes to the console." << '\n';
cfs::closecppfs();
return 0;
}This program:
- reads
nfromtest.in - writes
ntotest.out - writes the message to the console through
std::cout
cfs.hpp defines:
static std::ifstream cfs::cin;
static std::ofstream cfs::cout;Because they are file streams:
-
cfs::cinbehaves likestd::ifstream -
cfs::coutbehaves likestd::ofstream
Example:
int x;
double y;
std::string s;
cfs::cin >> x >> y >> s;
cfs::cout << x << ' ' << y << ' ' << s << '\n';cfs::cppfs() does not redirect standard streams.
This means:
std::cin
std::cout
scanf
printfare not affected.
Only:
cfs::cin
cfs::coutuse the files opened by cfs::cppfs().
For cfs::cppfs, both parameters should be valid file names.
Recommended:
cfs::cppfs("test.in", "test.out");Do not pass NULL or nullptr to cfs::cppfs.
If you only need to redirect one side, use cfs::cfs() instead.
If cfs::cin or cfs::cout is already open, calling cfs::cppfs() again will not reopen the streams.
Example:
cfs::cppfs("a.in", "a.out");
cfs::cppfs("b.in", "b.out");The second call will produce an error message:
Error-from-cfs Already open cppfs,please close first!
Correct usage:
cfs::cppfs("a.in", "a.out");
cfs::closecppfs();
cfs::cppfs("b.in", "b.out");
cfs::closecppfs();void cfs::closecppfs();This function closes cfs::cin and cfs::cout if they are open.
Example:
#include "cfs.hpp"
int main() {
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;
cfs::cout << n << '\n';
cfs::closecppfs();
return 0;
}After calling cfs::closecppfs(), do not continue using cfs::cin or cfs::cout unless you call cfs::cppfs() again.
const char* cfs::getversion();This function returns the current cfstream version as a const char*.
Example:
#include <iostream>
#include "cfs.hpp"
int main() {
std::cout << cfs::getversion() << '\n';
return 0;
}For version 1.3.0, the output is:
1.3.0
For most C++ programs, especially competitive programming or local testing, the recommended style is:
#include "cfs.hpp"
int main() {
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;
cfs::cout << n << '\n';
cfs::closecppfs();
return 0;
}This keeps file I/O and console I/O separate.
Use cfs::cfs() only when you want behavior similar to freopen.
Example:
#include <bits/stdc++.h>
#include "cfs.hpp"
using namespace std;
#define int long long
#define endl '\n'
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;
cfs::cout << n << endl;
cfs::closecppfs();
return 0;
}Note that in this example:
- unqualified
cinmeansstd::cin - unqualified
coutmeansstd::cout -
cfs::cinmeans the cfstream input file stream -
cfs::coutmeans the cfstream output file stream
Because the project uses cfs::cin and cfs::cout, it is usually clearer to keep the cfs:: prefix.
In cfs.hpp, cfs::cin and cfs::cout are declared with static.
This gives them internal linkage.
In practice, each translation unit that includes cfs.hpp gets its own private copy of:
cfs::cin
cfs::coutThis is intentional for cfstream.
It means file stream state is not shared globally across different .cpp files.
Incorrect:
cfs::cppfs("a.in", "a.out");
cfs::cppfs("b.in", "b.out");Correct:
cfs::cppfs("a.in", "a.out");
cfs::closecppfs();
cfs::cppfs("b.in", "b.out");
cfs::closecppfs();Incorrect:
cfs::cppfs(NULL, "test.out");Use cfs::cfs() instead:
cfs::cfs(NULL, "test.out");or in C++11 or later:
cfs::cfs(nullptr, "test.out");cfs::cppfs() opens cfs::cin, not std::cin.
Incorrect:
cfs::cppfs("test.in", "test.out");
int n;
std::cin >> n;Correct:
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;Recommended:
cfs::cppfs("test.in", "test.out");
// use cfs::cin and cfs::cout
cfs::closecppfs();Although file streams may be closed automatically when the program exits, explicitly closing them makes the program behavior clearer.
closecppfs() only closes cfs::cin and cfs::cout.
It does not affect standard streams.
For standard stream redirection, use:
cfs::closecfs();| Function / Object | Type | Description |
|---|---|---|
cfs::getversion() |
Function | Returns the current cfstream version |
cfs::cfs(input, output) |
Function | Redirects standard input/output |
cfs::closecfs() |
Function | Restores standard input/output to the console |
cfs::cppfs(input, output) |
Function | Opens independent C++ file streams |
cfs::closecppfs() |
Function | Closes independent C++ file streams |
cfs::cin |
std::ifstream |
Input file stream used by cppfs
|
cfs::cout |
std::ofstream |
Output file stream used by cppfs
|
Continue with: