std::freopen(3)
πΈ NAME πΈ
std::freopen β bestie, it's literally std::freopen. That's the whole name. Iconic of it tbh π
Ok so real talk, this lives in the C++ Standard Library and we're gonna get into it. Strap in π
β¨ Synopsis β¨
Defined in header <cstdio>:
std::FILE* freopen( const char* filename, const char* mode, std::FILE* stream );
Here's the tea on what it actually does, step by step β:
First, it attempts to close the file associated with stream, ignoring any errors (yeah, it does not care, it's moving on π). Then, if filename is not null, it attempts to open the file specified by filename using mode as if by std::fopen, and associates that file with the file stream pointed to by stream.
And if filename is a null pointer? Then the function attempts to reopen the file that is already associated with stream β but heads up, it is implementation defined which mode changes are allowed in this case π
π«Ά Parameters π«Ά
filenameβ file name to associate the file stream tomodeβ null-terminated character string determining new file access modestreamβ the file stream to modify
π File access flags π
Ok this is the part everyone screenshots, so let me lay out every single mode string with its meaning, its explanation, what happens if the file already exists, and what happens if it doesn't π
"r"β read β Open a file for reading β if it already exists: read from start β if it does not exist: returnNULLand set error π"w"β write β Create a file for writing β if it already exists: destroy contents β if it does not exist: create new"a"β append β Append to a file β if it already exists: write to end β if it does not exist: create new"r+"β read extended β Open a file for read/write β if it already exists: read from start β if it does not exist: returnNULLand set error π"w+"β write extended β Create a file for read/write β if it already exists: destroy contents β if it does not exist: create new"a+"β append extended β Open a file for read/write β if it already exists: write to end β if it does not exist: create new
File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has no effect on POSIX systems, but on Windows, for example, it disables special handling of '\n' and '\x1A' πͺ
On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator. No cap, it just goes to the end π
File access mode flag "x" can optionally be appended to "w" or "w+" specifiers. This flag forces the function to fail if the file exists, instead of overwriting it. (C++17) π
β οΈ The behavior is undefined if the mode is not one of the strings listed above. Some implementations define additional supported modes (e.g. Windows). So likeβ¦ don't freestyle the mode string, bestie π
π Return value π
stream on success, a null pointer on failure. That's it. Two outcomes. Slay π
π¦ Notes π¦
std::freopen is the only way to change the narrow/wide orientation of a stream once it has been established by an I/O operation or by std::fwide. Literally the only one. Main character energy fr fr π
Microsoft CRT version of std::freopen does not support any mode changes when filename is a null pointer and treats this as an error (see documentation). A possible workaround is the non-standard function _setmode(). Noted π«‘
π Example π
The following code redirects stdout to a file.
// Run this code
#include <cstdio>
int main()
{
std::printf("stdout is printed to console\n");
if (std::freopen("redir.txt", "w", stdout))
{
std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
std::fclose(stdout);
}
}
β Output β
stdout is printed to console
πΈ See also πΈ
fopenβ opens a file (function)fcloseβ closes a file (function)- C documentation for
freopen
That's the whole page, bestie, go forth and redirect your streams π§ββοΈπ
This was brought to you by http://cppreference.com, dated 2024.06.10, filed under std::freopen(3) β mwah, catch you on the next manpage πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms