Skip to content

Getting Started

weigao edited this page Jun 20, 2026 · 2 revisions

Getting Started

This page explains how to install and start using cfstream in a C or C++ project.

cfstream is a lightweight header-only library. You do not need to build or link any extra source files. Just include the correct header file in your project and start using it.

Requirements

C++

For C++ projects, use:

#include "cfs.hpp"

Recommended standard:

  • C++98 or later

The C++ version provides both standard stream redirection and independent C++ file streams.

C

For C projects, use:

#include "cfs.h"

Required standard:

  • C99 or later

If the C standard is lower than C99, cfs.h will stop compilation with #error.

Download

Download the latest release package from the release page:

After downloading, extract the package and choose the header file you need.

Choose the Correct Header

Header Language Description
cfs.hpp C++ C++ interface for cfstream
cfs.h C C interface for cfstream

Use cfs.hpp in C++ source files.

Use cfs.h in C source files.

In normal use, do not include both headers in the same source file.

Add the Header to Your Project

The recommended way is to place the header file in the same directory as your source file.

For C++:

your-project/
├── main.cpp
└── cfs.hpp

For C:

your-project/
├── main.c
└── cfs.h

Then include the header with double quotes.

C++:

#include "cfs.hpp"

C:

#include "cfs.h"

You can also place the header in your compiler include path and include it with angle brackets:

#include <cfs.hpp>
#include <cfs.h>

However, for most small projects, placing the header directly in the project directory is simpler and recommended.

Quick Start for C++

The C++ version provides two main ways to use cfstream.

Method 1: Independent C++ File Streams

This is the recommended C++ style when you want to keep standard input and output unchanged.

#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 program reads from test.in and writes to test.out.

In this mode:

  • cfs::cin reads from the input file
  • cfs::cout writes to the output file
  • std::cin and std::cout are not redirected

This is useful when you want to use file streams and console streams separately.

Method 2: Redirect Standard Streams

You can also redirect standard input and output directly.

#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 redirects standard input and output.

In this mode:

  • std::cin reads from test.in
  • std::cout writes to test.out

This style is close to using freopen, but wrapped by cfstream.

Quick Start for C

The C version redirects standard input and output.

#include <stdio.h>
#include "cfs.h"

int main(void) {
    cfs("test.in", "test.out");

    int n;
    scanf("%d", &n);
    printf("%d\n", n);

    closecfs();

    return 0;
}

This program reads from test.in and writes to test.out.

In this mode:

  • scanf reads from the input file
  • printf writes to the output file

Redirect Only Input or Output

Both input and output file names can be NULL.(in C++ it can also be nullptr

If a parameter is NULL, the corresponding stream will not be redirected.

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;
}

In this example:

  • input still comes from the console
  • output is written to test.out

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;
}

In this example:

  • input is read from test.in
  • output still goes to the console

Check the Version

cfstream provides a function to get the current library version.

C++

#include <iostream>
#include "cfs.hpp"

int main() {
    std::cout << cfs::getversion() << '\n';

    return 0;
}

C

#include <stdio.h>
#include "cfs.h"

int main(void) {
    printf("%s\n", cfs_getversion());

    return 0;
}

The version is returned as a const char*.

Basic Notes

Remember to close the stream when you no longer need it.

For C++ independent file streams:

cfs::closecppfs();

For standard stream redirection in C++:

cfs::closecfs();

For C:

closecfs();

Closing the stream restores or closes the corresponding stream resources.

Recommended Next Pages

After finishing this page, continue with:

Clone this wiki locally