Skip to content

Cpp Usage

weigao edited this page Jun 20, 2026 · 5 revisions

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.

Header

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();

Requirements

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.

Namespace

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.

Two C++ Usage Modes

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.

Mode 1: Standard Stream Redirection

Function

void cfs::cfs(const char* input, const char* output);

This function redirects standard input and/or standard output.

Internally, it uses freopen.

Basic 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();
    return 0;
}

This program:

  • reads from test.in
  • writes to test.out

What Is Redirected

After calling:

cfs::cfs("test.in", "test.out");

the following streams/functions are affected:

std::cin
std::cout
scanf
printf

They will use the redirected standard input/output streams.

Redirect Only Input

#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);

Redirect Only Output

#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");

No Redirection

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.

Close File Redirection and Restore Console Streams

Function

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.

Mode 2: Independent C++ File Streams

Function

void cfs::cppfs(const char* input, const char* output);

This function opens two independent C++ file streams:

cfs::cin
cfs::cout

They are separate from:

std::cin
std::cout

Basic Example

#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 n from test.in
  • writes n to test.out
  • writes the message to the console through std::cout

Streams

cfs.hpp defines:

static std::ifstream cfs::cin;
static std::ofstream cfs::cout;

Because they are file streams:

  • cfs::cin behaves like std::ifstream
  • cfs::cout behaves like std::ofstream

Example:

int x;
double y;
std::string s;

cfs::cin >> x >> y >> s;
cfs::cout << x << ' ' << y << ' ' << s << '\n';

Important Difference from Standard Redirection

cfs::cppfs() does not redirect standard streams.

This means:

std::cin
std::cout
scanf
printf

are not affected.

Only:

cfs::cin
cfs::cout

use the files opened by cfs::cppfs().

File Names Must Be Valid

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.

Reopening Protection

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();

Close Independent File Streams

Function

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.

Version Query

Function

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

Recommended Usage Style

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.

Using cfstream with OI

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 cin means std::cin
  • unqualified cout means std::cout
  • cfs::cin means the cfstream input file stream
  • cfs::cout means the cfstream output file stream

Because the project uses cfs::cin and cfs::cout, it is usually clearer to keep the cfs:: prefix.

Multiple Source Files

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::cout

This is intentional for cfstream.

It means file stream state is not shared globally across different .cpp files.

Common Mistakes

1. Calling cppfs twice without closing

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();

2. Passing NULL or nullptr to cppfs

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");

3. Confusing std::cin with cfs::cin

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;

4. Forgetting to close file streams

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.

5. Expecting closecppfs to restore std::cin or std::cout

closecppfs() only closes cfs::cin and cfs::cout.

It does not affect standard streams.

For standard stream redirection, use:

cfs::closecfs();

Function Summary

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

Next Pages

Continue with:

Clone this wiki locally