Compartir a través de


fopen, _wfopen (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Open a file.

FILE *fopen(    const char*filename,constchar*mode);FILE *_wfopen(    const wchar_t*filename,const wchar_t*mode);

Parameters

  • filename
    Filename.
  • mode
    Type of access permitted.

Return Values

Each of these functions returns a pointer to the open file.

A null pointer value indicates an error.

Remarks

The fopen function opens the file specified by filename.

_wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. Otherwise, _wfopen and fopen behave identically.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE Defined
_tfopen _wfopen

For more information about TCHAR.H routines, see Generic Text Mappings.

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 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 file exists, its contents are destroyed.

  • "a+"
    Opens for reading and appending; the appending operation includes the removal of the EOF marker before 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.

    When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file.

    The file pointer can be repositioned using fseek, but is always moved back to the end of the file before a write operation is carried out. Thus, existing data cannot be overwritten.

    The "a" mode does not remove the EOF marker before appending to the file. After appending occurs, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file.

    The "a+" mode removes the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file.

    The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

    When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, or fseek operation.

    The current position can be specified for the fsetpos or fseek operation.

    In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:

  • t
    Open in text (translated) mode.

    In this mode, CTRL+Z is interpreted as an end-of-file character on input.

    In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible.

    This is done because using fseek and ftell to move within a file that ends with a CTRL+Z can cause fseek to behave improperly near the end of the file.

    Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output.

    When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters.

    For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters.

  • b
    Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

    If t or b is not given in mode, the default translation mode is defined by the global variable _fmode.

    If t or b is prefixed to the argument, the function fails and returns NULL.

  • c
    Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if fflush or _flushall is called.

  • n
    Reset the commit flag for the associated filename to "no-commit." This is the default.

Example

/* FOPEN.C: This program opens files named "data"
 * and "data2".It  uses fclose to close "data" and
 * _fcloseall to close all remaining files.
 */

#include <stdio.h>

FILE *stream, *stream2;

void main( void )
{
   int numclosed;

   /* Open for read (will fail if file "data" does not exist) */
   if( (stream  = fopen( "data", "r" )) == NULL )
      printf( "The file 'data' was not opened\n" );
   else
      printf( "The file 'data' was opened\n" );

   /* Open for write */
   if( (stream2 = fopen( "data2", "w+" )) == NULL )
      printf( "The file 'data2' was not opened\n" );
   else
      printf( "The file 'data2' was opened\n" );

   /* Close stream */
   if( fclose( stream ) )
      printf( "The file 'data' was not closed\n" );

   /* All other files are closed: */
   numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}

Output

The file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1

Requirements

OS Versions: Windows CE 2.0 and later.
Header: stdlib.h.
Link Library: coredll.dll.

See Also

fclose | ferror | _fileno, _setmode

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.