type
<cstdio>
FILE
Object containing information to control a stream
Object type that identifies a stream and contains the information needed to control it, including a pointer to its buffer, its position indicator and all its state indicators.
FILE objects are usually created by a call to either fopen or tmpfile, which both return a pointer to one of these objects.
The content of a FILE object is not meant to be accessed from outside the functions of the <cstdio> and <cwchar> headers; In fact, portable programs shall only use them in the form of pointers to identify streams, since for some implementations, even the value of the pointer itself could be significant to identify the stream (i.e., the pointer to a copy of a FILE object could be interpreted differently than a pointer to the original).
Its memory allocation is automatically managed: it is allocated by either fopen or tmpfile, and it is the responsibility of the library to free the resources once either the stream has been closed using fclose or the program terminates normally.
On inclusion of the <cstdio> header file, three objects of this type are automatically created, and pointers to them are declared: stdin, stdout and stderr, associated with the standard input stream, standard output stream and standard error stream, respectively.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
/* FEOF example */
#include <stdio.h>
int main()
{
FILE * pFile;
char buffer [100];
pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
while ( ! feof (pFile) )
{
if ( fgets (buffer , 100 , pFile) == NULL ) break;
fputs (buffer , stdout);
}
fclose (pFile);
}
return 0;
}
| |
This example reads the content of a text file called myfile.txt and sends it to the standard output stream.
See also
- fopen
- Open file (function
)
- fclose
- Close file (function
)