C Reference ยท stdio.h
The C FILE type
Reference for the C FILE type: an opaque stream object you handle through FILE pointers, what it holds, how to pass it around and why you never copy it.
FILE is the type that represents a stream in the C standard library: a source or destination of bytes such as a disk file, the keyboard, the screen or an in-memory buffer. You never create a FILE yourself. Functions such as fopen() allocate one and give you a pointer to it; you pass that FILE * to every other I/O function and finally give it back with fclose().
The type is opaque: the standard does not say what is inside. Typically it holds the file descriptor, the buffer, the current position, and the end-of-file and error flags.
#include <stdio.h> FILE *fp = fopen("data.txt", "r");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
FILE is defined in <stdio.h>. You use it only through pointers.
FILE *fopen(const char *path, const char *mode); int fclose(FILE *stream); extern FILE *stdin, *stdout, *stderr;
| Name | Description |
|---|---|
FILE |
Opaque object describing a stream. Never declare a variable of type FILE and never copy one. |
FILE * |
The handle all stream functions take and return. A NULL value means "no stream" (for example when fopen() fails). |
stdin, stdout, stderr |
Three FILE * expressions that are open when main() starts. See stdin, stdout and stderr. |
Returns / errors: a FILE * produced by fopen(), tmpfile(), freopen(), fmemopen() or open_memstream() is valid until the matching close. Always compare it with NULL before use.
Undefined behavior: using a FILE * after fclose(), passing NULL to most stream functions, and copying the contents of a FILE object (for example with memcpy or *b = *a; the type may even be incomplete) are all undefined.
Usage notes
- A stream is not a file. A stream adds buffering, a position and status flags on top of whatever sits underneath (a file, a pipe, a terminal, memory).
- Compare pointers, not objects. Two
FILE *values refer to the same stream only if they are equal. Assigning one to another copies the pointer, not the stream. - Each stream has two flags, end-of-file and error, which are queried with
feof()andferror()and cleared withclearerr()or by repositioning. - Buffering is per stream: see
setvbufandfflush. - Always close what you open. An open stream consumes a file descriptor (operating systems limit these, often to a thousand or so per process) and may hold unwritten data.
fclose()flushes it. - Passing streams to functions is the way to write code that works on any destination: a helper taking
FILE *outcan print to the screen, a log file or an in-memory buffer without change. - Threads: the standard stream functions lock the stream internally in glibc, so concurrent use is safe but slow;
getc_unlocked()and friends are POSIX escape hatches. - Do not rely on the layout. Old code that peeked at
fp->_cntor similar members is not portable and breaks with every C library.
Related
fopen,fclose,stdin,stdoutandstderr,freopen,tmpfile- Opaque types: Opaque types
- Lessons: File handling basics, Text files