This shows you the differences between two versions of the page.
— |
c:stdio.h:fclose [2024/02/16 01:05] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{keywords>wiki library source code example reference}} | ||
+ | ===== fclose ===== | ||
+ | <code c> | ||
+ | #include <stdio.h> /* including standard library */ | ||
+ | |||
+ | int fclose( FILE *stream ); | ||
+ | </code> | ||
+ | |||
+ | === Description === | ||
+ | Close the current stream / file\\ | ||
+ | Used to close the specified file (stream). You should always use this function, to clean up after using a file. \\ | ||
+ | Any data that has been written to the file, but not actually made it that far yet, i.e. which has only been written to the file buffer in memory, \\ | ||
+ | will then be actually written to the physical file itself by the operating system. \\ | ||
+ | The space in memory allocated for these buffers will also be deallocated. Any user defined buffers (specified by setbuf and setvbuf will however, not be deallocated.\\ | ||
+ | Any temp files associated with the stream will also be deleted.\\ | ||
+ | \\ | ||
+ | Returns EOF on error, zero otherwise.\\ | ||
+ | |||
+ | ===== C Sourcecode Example ===== | ||
+ | <code c> | ||
+ | /* | ||
+ | * fclose example code | ||
+ | * http://code-reference.com/c/stdio.h/fclose | ||
+ | */ | ||
+ | #include <stdio.h> /* including standard library */ | ||
+ | //#include <windows.h> /* uncomment this for Windows */ | ||
+ | |||
+ | |||
+ | int main( void ) | ||
+ | { | ||
+ | FILE *handle; | ||
+ | handle = fopen("test.txt","r"); | ||
+ | if ( handle != 0 ) | ||
+ | { | ||
+ | printf("fopen success:\n"); | ||
+ | fclose(handle); | ||
+ | } else { | ||
+ | printf("fopen failure\n"); | ||
+ | |||
+ | if (handle != NULL) { | ||
+ | fclose(handle); | ||
+ | } | ||
+ | return 1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ==== fclose output ==== | ||
+ | output: if no test.txt file there | ||
+ | user@host:~$ ./fclose | ||
+ | fopen failure | ||
+ | |||
+ | output: if test.txt exist | ||
+ | user@host:~$ ./fclose | ||
+ | fopen success | ||
+ | |||
+ | ==== see also ==== | ||
+ | [[c:stdio.h:fopen|fopen()]] [[c:stdio.h:freopen|freopen()]] [[c:stdio.h:fflush|fflush()]] |