-
Notifications
You must be signed in to change notification settings - Fork 1
API Reference
This page provides the complete API reference for cfstream.
It is intended as a lookup page for function declarations, parameters, return values, behavior, and important notes.
For tutorial-style documentation, see:
cfstream provides two public headers:
| Header | Language | Main Purpose |
|---|---|---|
cfs.hpp |
C++ | C++ interface with namespace support and independent C++ file streams |
cfs.h |
C | C interface for standard input/output redirection |
Current version:
1.3.0
The C++ API is provided by:
#include "cfs.hpp"All C++ APIs are inside the cfs namespace.
namespace cfs {
static std::ifstream cin;
static std::ofstream cout;
static inline const char* getversion();
static inline void cfs(const char* input, const char* output);
static inline void closecfs();
static inline void cppfs(const char* input, const char* output);
static inline void closecppfs();
}static inline const char* cfs::getversion();Returns the current cfstream version.
None.
| Type | Description |
|---|---|
const char* |
Current cfstream version string |
For version 1.3.0, the returned string is:
1.3.0
This function does not perform file operations.
#include <iostream>
#include "cfs.hpp"
int main() {
std::cout << cfs::getversion() << '\n';
return 0;
}static inline void cfs::cfs(const char* input, const char* output);Redirects standard input and/or standard output.
This function is similar to using freopen directly, but wrapped by cfstream.
| Parameter | Type | Description |
|---|---|---|
input |
const char* |
Input file name. If NULL, input is not redirected. |
output |
const char* |
Output file name. If NULL, output is not redirected. |
In C++11 or later, nullptr can also be used instead of NULL.
None.
Internally, this function uses:
std::freopen(input, "r", stdin);
std::freopen(output, "w", stdout);when the corresponding parameter is not null.
After successful redirection, the following streams/functions are affected:
std::cin
std::cout
scanf
printfThey will use the redirected standard input/output streams.
- Passing
NULLornullptrmeans the corresponding stream will not be redirected. - Passing both parameters as
NULLornullptris allowed, but has no practical effect. - This function does not open
cfs::cinorcfs::cout. - Use
cfs::closecfs()to restore standard streams to the console.
#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;
}static inline void cfs::closecfs();Ends the file redirection created by cfs::cfs() and attempts to restore standard input and output back to the console.
None.
None.
Internally, this function redirects stdin and stdout back to platform-specific console devices.
| Platform | Console Device |
|---|---|
| Windows | CON |
| Linux / macOS / other Unix-like systems | /dev/tty |
- This function does not permanently close
std::cinorstd::cout. - It redirects the standard streams back to the console device.
- It does not affect
cfs::cinorcfs::cout. - For independent C++ file streams, use
cfs::closecppfs()instead.
#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;
}static inline void cfs::cppfs(const char* input, const char* output);Opens independent C++ file streams:
cfs::cin
cfs::coutThese streams are separate from standard streams.
| Parameter | Type | Description |
|---|---|---|
input |
const char* |
Input file name for cfs::cin
|
output |
const char* |
Output file name for cfs::cout
|
None.
If neither cfs::cin nor cfs::cout is open, this function opens:
cfs::cin.open(input);
cfs::cout.open(output);If either cfs::cin or cfs::cout is already open, this function does not reopen them and prints an error message to std::cerr:
Error-from-cfs Already open cppfs,please close first!
-
cfs::cppfs()does not redirectstd::cinorstd::cout. -
scanfandprintfare not affected. - Do not pass
NULLornullptrtocfs::cppfs(). - If you need single-side redirection, use
cfs::cfs()instead. - Use
cfs::closecppfs()before callingcfs::cppfs()again.
#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;
}static inline void cfs::closecppfs();Closes the independent C++ file streams opened by cfs::cppfs().
None.
None.
This function checks whether the streams are open and closes them if needed:
if (cin.is_open()) cin.close();
if (cout.is_open()) cout.close();- This function only affects
cfs::cinandcfs::cout. - It does not affect
std::cin,std::cout,scanf, orprintf. - After calling
cfs::closecppfs(), do not continue usingcfs::cinorcfs::coutunless you callcfs::cppfs()again.
#include "cfs.hpp"
int main() {
cfs::cppfs("test.in", "test.out");
int n;
cfs::cin >> n;
cfs::cout << n << '\n';
cfs::closecppfs();
return 0;
}static std::ifstream cfs::cin;Input file stream used by cfs::cppfs().
std::ifstreamAfter calling:
cfs::cppfs("test.in", "test.out");cfs::cin reads from test.in.
-
cfs::cinis independent fromstd::cin. - It is only opened by
cfs::cppfs(). - It is closed by
cfs::closecppfs(). - It has internal linkage because it is declared with
static.
int n;
cfs::cin >> n;static std::ofstream cfs::cout;Output file stream used by cfs::cppfs().
std::ofstreamAfter calling:
cfs::cppfs("test.in", "test.out");cfs::cout writes to test.out.
-
cfs::coutis independent fromstd::cout. - It is only opened by
cfs::cppfs(). - It is closed by
cfs::closecppfs(). - It has internal linkage because it is declared with
static.
cfs::cout << "Hello cfstream" << '\n';The C API is provided by:
#include "cfs.h"The C API does not use a namespace.
static inline const char* cfs_getversion(void);
static inline void cfs(const char* input, const char* output);
static inline void closecfs(void);static inline const char* cfs_getversion(void);Returns the current cfstream version.
None.
| Type | Description |
|---|---|
const char* |
Current cfstream version string |
For version 1.3.0, the returned string is:
1.3.0
This function does not perform file operations.
#include "cfs.h"
int main(void) {
printf("%s\n", cfs_getversion());
return 0;
}static inline void cfs(const char* input, const char* output);Redirects standard input and/or standard output.
This function is similar to using freopen directly, but wrapped by cfstream.
| Parameter | Type | Description |
|---|---|---|
input |
const char* |
Input file name. If NULL, input is not redirected. |
output |
const char* |
Output file name. If NULL, output is not redirected. |
None.
Internally, this function uses:
freopen(input, "r", stdin);
freopen(output, "w", stdout);when the corresponding parameter is not NULL.
After successful redirection, the following functions are affected:
scanf
printfThey will use the redirected standard input/output streams.
- Passing
NULLmeans the corresponding stream will not be redirected. - C does not support
nullptr. - Passing both parameters as
NULLis allowed, but has no practical effect. - Use
closecfs()to restore standard streams to the console.
#include "cfs.h"
int main(void) {
cfs("test.in", "test.out");
int n;
scanf("%d", &n);
printf("%d\n", n);
closecfs();
return 0;
}static inline void closecfs(void);Ends the file redirection created by cfs() and attempts to restore standard input and output back to the console.
None.
None.
Internally, this function redirects stdin and stdout back to platform-specific console devices.
| Platform | Console Device |
|---|---|
| Windows | CON |
| Linux / macOS / other Unix-like systems | /dev/tty |
- This function does not permanently close
stdinorstdout. - It redirects the standard streams back to the console device.
- It should be used after
cfs()when you no longer need file redirection.
#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;
}| Interface | Parameter |
NULL Allowed |
nullptr Allowed |
Notes |
|---|---|---|---|---|
cfs::cfs |
input |
Yes | C++11 or later | If null, input is not redirected |
cfs::cfs |
output |
Yes | C++11 or later | If null, output is not redirected |
cfs |
input |
Yes | No | C does not support nullptr
|
cfs |
output |
Yes | No | C does not support nullptr
|
cfs::cppfs |
input |
Not recommended | Not recommended | Use a valid file name |
cfs::cppfs |
output |
Not recommended | Not recommended | Use a valid file name |
| API | Return Type | Meaning |
|---|---|---|
cfs::getversion() |
const char* |
Current cfstream version |
cfs_getversion() |
const char* |
Current cfstream version |
cfs::cfs() |
void |
No return value |
cfs::closecfs() |
void |
No return value |
cfs::cppfs() |
void |
No return value |
cfs::closecppfs() |
void |
No return value |
cfs() |
void |
No return value |
closecfs() |
void |
No return value |
If cfs::cppfs() is called while cfs::cin or cfs::cout is already open, it prints this message to std::cerr:
Error-from-cfs Already open cppfs,please close first!
The function then returns without reopening the streams.
If cfs.h is compiled as C with a standard lower than C99, compilation stops with:
cfs.h requires C99 or later.
closecfs() and cfs::closecfs() use platform-specific console devices to restore standard streams.
| Platform |
stdin Restore Target |
stdout Restore Target |
|---|---|---|
| Windows | CON |
CON |
| Linux / macOS / other Unix-like systems | /dev/tty |
/dev/tty |
In cfs.hpp, cfs::cin and cfs::cout are declared with static.
This gives them internal linkage.
Each translation unit that includes cfs.hpp gets its own private copy of:
cfs::cin
cfs::coutIn cfs.h, the functions are defined as static inline.
This allows the header to be included in multiple C source files without external linker conflicts.
When cfs.h is included from C++ code, the C interface is wrapped with extern "C".