Files and IO streams
suggest changeSyntax
- #include <stdio.h> /* Include this to use any of the following sections */
- FILE *fopen(const char *path, const char *mode); /* Open a stream on the file at path with the specified mode */
- FILE *freopen(const char *path, const char *mode, FILE *stream); /* Re-open an existing stream on the file at path with the specified mode */
- int fclose(FILE *stream); /* Close an opened stream */
- size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); /* Read at most nmemb elements of size bytes each from the stream and write them in ptr. Returns the number of read elements. */
- size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); /* Write nmemb elements of size bytes each from ptr to the stream. Returns the number of written elements. */
- int fseek(FILE *stream, long offset, int whence); /* Set the cursor of the stream to offset, relative to the offset told by whence, and returns 0 if it succeeded. */
- long ftell(FILE *stream); /* Return the offset of the current cursor position from the beginning of the stream. */
- void rewind(FILE *stream); /* Set the cursor position to the beginning of the file. */
- int fprintf(FILE *fout, const char *fmt, …); /* Writes printf format string on fout */
- FILE *stdin; /* Standard input stream */
- FILE *stdout; /* Standard output stream */
- FILE *stderr; /* Standard error stream */
Parameters
Parameter | Details |
——— | —–– |
const char *mode | A string describing the opening mode of the file-backed stream. See remarks for possible values. |
int whence | Can be SEEK_SET
to set from the beginning of the file, SEEK_END
to set from its end, or SEEK_CUR
to set relative to the current cursor value. Note: SEEK_END
is non-portable.|
Remarks
Mode strings:
Mode strings in fopen()
and freopen()
can be one of those values:
"r"
: Open the file in read-only mode, with the cursor set to the beginning of the file."r+"
: Open the file in read-write mode, with the cursor set to the beginning of the file."w"
: Open or create the file in write-only mode, with its content truncated to 0 bytes. The cursor is set to the beginning of the file."w+"
: Open or create the file in read-write mode, with its content truncated to 0 bytes. The cursor is set to the beginning of the file."a"
: Open or create the file in write-only mode, with the cursor set to the end of the file."a+"
: Open or create the file in read-write mode, with the read-cursor set to the beginning of the file. The output, however, will always be appended to the end of the file.
Each of these file modes may have a b
added after the initial letter (e.g. "rb"
or "a+b"
or "ab+"
). The b
means that the file should be treated as a binary file instead of a text file on those systems where there is a difference. It doesn’t make a difference on Unix-like systems; it is important on Windows systems. (Additionally, Windows fopen
allows an explicit t
instead of b
to indicate ‘text file’ — and numerous other platform-specific options.)
"wx"
: Create a text file in write-only mode. The file may not exist."wbx"
: Create a binary file in write-only mode. The file may not exist.
The x
, if present, must be the last character in the mode string.