-
Notifications
You must be signed in to change notification settings - Fork 1
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.
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.
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 the latest release package from the release page:
After downloading, extract the package and choose the header file you need.
| 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.
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.
The C++ version provides two main ways to use cfstream.
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::cinreads from the input file -
cfs::coutwrites to the output file -
std::cinandstd::coutare not redirected
This is useful when you want to use file streams and console streams separately.
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::cinreads fromtest.in -
std::coutwrites totest.out
This style is close to using freopen, but wrapped by cfstream.
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:
-
scanfreads from the input file -
printfwrites to the output file
Both input and output file names can be NULL.
If a parameter is NULL, the corresponding stream will not be redirected.
#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
#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
cfstream provides a function to get the current library version.
#include <iostream>
#include "cfs.hpp"
int main() {
std::cout << cfs::getversion() << '\n';
return 0;
}#include <stdio.h>
#include "cfs.h"
int main(void) {
printf("%s\n", cfs_getversion());
return 0;
}The version is returned as a const char*.
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.
After finishing this page, continue with: