basic_filebuf::open

basic_filebuf *open(const char *s,
    ios_base::openmode mode);

The member function endeavors to open the file with filename s, by calling fopen(s, strmode). Here strmode is determined from mode & ~(ate & | binary):

  • ios_base::in becomes “r” (open existing file for reading).
  • ios_base::out or ios_base::out | ios_base::trunc becomes “w” (truncate existing file or create for writing).
  • ios_base::out | app becomes “a” (open existing file for appending all writes).
  • ios_base::in | ios_base::out becomes “r+” (open existing file for reading and writing).
  • ios_base::in | ios_base::out | ios_base::trunc becomes “w+” (truncate existing file or create for reading and writing).
  • ios_base::in | ios_base::out | ios_base::app becomes “a+” (open existing file for reading and for appending all writes).

If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END to position the stream at end-of-file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.

If the file pointer is not a null pointer, the function determines the file conversion facet: use_facet< codecvt<E, char, traits_type:: state_type> >(getloc()), for use by underflow and overflow.

If the file pointer is a null pointer, the function returns a null pointer. Otherwise, it returns this.