Programming Reference/Librarys
Question & Answer
Q&A is closed
#include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ FILE * fopen (const char * restrict path, const char * restrict mode)
Opens file named restrict path and returns a stream, or NULL on failure. mode may be one of the following for files or one of those strings with b included (after the first character), for binary files.
Open a file
Arguments for mode are:
“r” Open a file for reading
“w” Create / writing
“a” Append
“b” Open a binary file
“rb” Open binary file for reading
”+” Open file for read/write
“r+” text update (reading and writing)
“w+” text update, discarding previous content (if any)
“a+” text append, reading, and writing at end
example fopen(“test.txt”, “r+”);, fopen(“test.txt”, “w+”); fopen(“test.txt”, “rw”); …
/* * fopen example code * http://code-reference.com/c/stdio.h/fopen */ #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; }
output: if no test.txt file there user@host:~$ ./fopen fopen failure
output: if test.txt exist user@host:~$ ./fopen fopen success