Once you've declare a file pointer to the file,you can use the function "fopen"
to open it.
the syntaxe for "fopen" is:
FILE *fopen( const char *filename, const char *mode );
the "mode" parameter can be one of those.
"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
An example:
FILE *fp; /*declaration of file pointer*/
fp = fopen("myfile.txt", "r" );
if( !fp )
printf("error while opening the file \"myfile.txt\"" /* checks if the file has been open properly */
this will open a file named "myfile.txt" in reading mode.
once you've finish using the file,you can close it by calling "fclose".
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.