freopen Function in C++ Explained

The freopen function is used to reassign the filename and opening mode of a file stream. Its usage is:

FILE* freopen(const char* filename, const char* mode, FILE* stream);
  1. File name: the name of the file that needs to be reassigned.
  2. mode: The mode for opening a file can be “r” (read-only), “w” (write, create a new file if it does not exist, clear the file content if it already exists), “a” (append, create a new file if it does not exist, append content to the end of the file), etc.
  3. stream: a file stream that requires reassigning the file name and opening mode.

The freopen function will close the current file stream, stream, and reassign it to a new file stream opening the filename file. It will return a pointer to the new file stream upon success, and NULL upon failure.

bannerAds