-
Notifications
You must be signed in to change notification settings - Fork 1
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.
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.
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.
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.
void cfs(const char* input, const char* output);This function redirects standard input and/or standard output.
Internally, it uses freopen.
#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
After calling:
cfs("test.in", "test.out");the following standard streams/functions are affected:
scanf
printfThey will use the redirected standard input/output streams.
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
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
Both parameters can be NULL.
cfs(NULL, NULL);This is allowed, but it has no practical effect.
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.
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
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.
cfs.h includes an extern "C" block when compiled in C++:
#ifdef __cplusplus
extern "C" {
#endifThis 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
cfsnamespace, you can includecfs.h. However, this is not recommended. The C header only provides functionality similar to standard stream redirection incfs.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.
Incorrect:
-std=c89
Correct:
-std=c99
or later:
-std=c11
-std=c17
-std=c23
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.
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.
closecfs() does not permanently destroy stdin or stdout.
It attempts to restore standard input and output back to the console device.
| 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 |
Continue with: