File access
Open file
std::fopen
std::FILE* fopen( const char* filename, const char* mode ); |
Opens the file indicated by filename
and returns the stream associated with the file. Use mode
to determine the file access mode.
Parameters
filename | – | The file name to which the file stream is to be associated |
mode | – | A null-terminated string that determines the file access mode |
|
Return value
If successful, returns a pointer to the object controlling the open file stream and clears the end-of-file and error bits. The stream is fully buffered unless filename refers to an interactive device.
On error, a null pointer is returned. POSIX requires errno to be set in this case.
Attention
The format of filename
is implementation-defined and does not necessarily refer to a file (for example, it could be the console or another device accessible through the file system API). On supported platforms, filename
can contain absolute or relative file system paths.
For portable directory and file naming, see the C++ filesystem library or boost.filesystem .
Call example
#include <cstdio> #include <cstdlib> int main() { FILE* fp = std::fopen("test.txt", "r"); if (!fp) { std::perror("File opening failed"); return EXIT_FAILURE; } int c; // Note: it is int instead of char, requiring EOF processing while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop { std::putchar(c); } if (std::ferror(fp)) { std::puts("I/O error when reading"); } else if (std::feof(fp)) { std::puts("End of file reached successfully"); } std::fclose(fp); return 0; }
Output
Open an existing stream with a different name
std::freopen
std::FILE* freopen( const char* filename, const char* mode, std::FILE* stream); |
First, an attempt is made to close the file associated with the stream
, ignoring any errors. Then, if filename
is not empty, try to use mode
to open the file specified by filename
, as if using fopen, and then combine the file with stream
. If filename
is a null pointer, the function attempts to reopen the file already associated with stream
(whether mode changes are allowed in this case is implementation-defined).
Parameters
filename | – | The file to which the file stream is to be associated |
mode | – | Null-terminated string that determines the new file opening mode |
|
stream | – | The file stream to be modified |
Return value
stream
on success, NULL on failure.
Attention
freopen
is the only way to change the narrow/wide orientation of a stream once the orientation has been established by an I/O operation or std::fwide.
Call example
#include <cstdio> int main() { std::printf("stdout is printed to console\ "); if (std::freopen("redir.txt", "w", stdout)) { std::printf("stdout is redirected to a file\ "); // This is written to redir.txt std::fclose(stdout); } return 0; }
Output
Close file
std::fclose
int fclose( std::FILE* stream ); |
Closes the given file stream. Flush any unwritten buffered data to the OS. Discard any unread buffered data.
Regardless of whether the operation is successful or not, the stream is no longer associated with the file, and buffers allocated by std::setbuf or std::setvbuf are also disassociated, if they exist, and deallocated if automatic allocation is used.
The behavior is undefined if the value of stream
is used after fclose
returns.
Parameters
stream | – | The file stream to be closed |
Return value
?0? on success, EOF otherwise.
Call example
#include <cstdio> #include <cstdlib> int main() { FILE* fp = std::fopen("test.txt", "r"); if (!fp) { std::perror("File opening failed"); return EXIT_FAILURE; } int c; // Note: it is int instead of char, requiring EOF processing while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop { std::putchar(c); } if (std::ferror(fp)) { std::puts("I/O error when reading"); } else if (std::feof(fp)) { std::puts("End of file reached successfully"); } std::fclose(fp); return 0; }