2.1.9.7 fopen
Description
Opens the file specified by filename.
Syntax
FILE * fopen( const char * filename, const char * mode )
Parameters
- filename
- Filename
- mode
- Type of access permitted
Return
returns a pointer to the open file. A null pointer value indicates an error.
Examples
EX1
//This following example open files named "data.txt", and "data2.txt". It uses fclose to close "data.txt" and "data2.txt"
void test_fopen()
{
FILE *stream, *stream2;
// Open for read (will fail if file "data" does not exist)
stream = fopen( "c:\\data.txt", "r" );
if( stream == NULL )
printf( "The file 'data' was not opened\n" );
else
printf( "The file 'data' was opened\n" );
// Open for write
stream2 = fopen( "c:\\data2.txt", "w+" );
if( stream2 == NULL )
printf( "The file 'data2' was not opened\n" );
else
printf( "The file 'data2' was opened\n" );
// Close stream
if ( stream != NULL )
{
if( fclose( stream ) )
printf( "The file 'data' was not closed\n" );
else
printf( "The file 'data' was closed\n" );
}
// Close stream2
if ( stream2 != NULL )
{
if( fclose( stream2 ) )
printf( "The file 'data2' was not closed\n" );
else
printf( "The file 'data2' was closed\n" );
}
}
Remark
This function opens the file specified by filename.
The character string mode specifies the type of access requested for the file, as follows:
"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.
If the filename doesn't contain file path, default of current path is %SYSTEM%\OriginC\OriginLab .
See Also
fclose
Header to Include
origin.h
Reference
|