Skip to content

C Usage

weigao edited this page Jun 21, 2026 · 3 revisions

C Usage

This page documents the C interface of cfstream, provided by cfs.h.

Use this page when you want to understand how the C version works in detail, including standard stream redirection, version query, closing behavior, and common usage rules.

Header

Use the C header:

#include "cfs.h"

The C interface provides these functions:

cfs(...);
closecfs();

cfs_getversion();

Unlike the C++ version, the C version does not use a namespace.

Requirements

cfs.h requires:

  • C99 or later

If the file is compiled as C and the C standard is lower than C99, compilation will stop with:

cfs.h requires C99 or later.

This check is implemented with #error.

Included Standard Header

cfs.h includes:

#include <stdio.h>

This means all of the functions in stdio.h such as scanf, printf are available after including cfs.h.

Basic Usage

Function

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

This function redirects standard input and/or standard output.

Internally, it uses freopen.

Example

#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
  • writes to test.out

What Is Redirected

After calling:

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

the following standard streams/functions are affected:

scanf
printf

They will use the redirected standard input/output streams.

Redirect Only Input

You can pass NULL as the output file name.

#include "cfs.h"

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

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

    closecfs();
    return 0;
}

In this example:

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

Redirect Only Output

You can pass NULL as the input file name.

#include "cfs.h"

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

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

    closecfs();
    return 0;
}

In this example:

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

No Redirection

Both parameters can be NULL.

cfs(NULL, NULL);

This is allowed, but it has no practical effect.

Close File Redirection and Restore Console Streams

Function

void closecfs(void);

This function ends the file redirection created by cfs() and attempts to restore standard input and output back to the console.

It does not permanently close stdin or stdout themselves. Instead, it redirects the standard streams back to the console device.

Example:

#include "cfs.h"

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

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

    closecfs();

    printf("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 standard streams.

Version Query

Function

const char* cfs_getversion(void);

This function returns the current cfstream version as a const char*.

Example:

#include "cfs.h"

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

For version 1.3.0, the output is:

1.3.0

Function Definitions and Linkage

In cfs.h, the functions are defined as static inline.

Example:

static inline void cfs(const char* input, const char* output);
static inline void closecfs(void);
static inline const char* cfs_getversion(void);

This design makes the header easy to include directly in C projects without requiring a separate .c implementation file.

Because the functions are static, each translation unit gets its own internal copy of these functions. This avoids external linker conflicts when the header is included by multiple source files.

C++ Compatibility

cfs.h includes an extern "C" block when compiled in C++:

#ifdef __cplusplus
extern "C" {
#endif

This allows the C header to be included by C++ code without C++ name mangling for the C interface.

However, for normal C++ projects, prefer using:

#include "cfs.hpp"

Use cfs.h mainly for C projects.

Tip: In C++, if you want to use cfstream without the cfs namespace, you can include cfs.h. However, this is not recommended. The C header only provides functionality similar to standard stream redirection in cfs.hpp, and because it does not use a namespace, it may cause naming conflicts in C++ projects. Issues caused by this usage may not be officially supported.

Common Mistakes

1. Using a C standard lower than C99

Incorrect:

-std=c89

Correct:

-std=c99

or later:

-std=c11
-std=c17
-std=c23

2. Expecting NULL to mean an empty file name

NULL does not mean an empty file name.

It means the corresponding stream will not be redirected.

Example:

cfs(NULL, "test.out");

This redirects only output.

3. Forgetting to call closecfs

Recommended:

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

/* use scanf and printf */

closecfs();

Although the operating system usually closes files when the program exits, explicitly calling closecfs() makes the program behavior clearer.

4. Thinking closecfs closes stdin and stdout permanently

closecfs() does not permanently destroy stdin or stdout.

It attempts to restore standard input and output back to the console device.

Function Summary

Function Return Type Description
cfs_getversion() const char* Returns the current cfstream version
cfs(input, output) void Redirects standard input/output
closecfs() void Restores standard input/output to the console

Next Pages

Continue with:

Clone this wiki locally