std::freopen(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 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 to
  • mode β€” null-terminated character string determining new file access mode
  • stream β€” 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: return NULL and 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: return NULL and 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