次の方法で共有


basic_filebuf::open

Opens a file.

basic_filebuf<Elem, Tr> *open(
    const char *_Filename,
    ios_base::openmode _Mode,
    int _Prot = (int)ios_base::_Openprot
);
basic_filebuf<Elem, Tr> *open(
    const char *_Filename,
    ios_base::openmode _Mode
);
basic_filebuf<Elem, Tr> *open(
    const wchar_t *_Filename,
    ios_base::openmode _Mode,
    int _Prot = (int)ios_base::_Openprot
);
basic_filebuf<Elem, Tr> *open(
    const wchar_t *_Filename,
    ios_base::openmode _Mode
);

Parameters

  • _Filename
    The name of the file to open.

  • _Mode
    One of the enumerations in ios_base::openmode.

  • _Prot
    The default file opening protection, equivalent to the shflag parameter in _fsopen, _wfsopen.

Return Value

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

Remarks

The member function opens the file with filename filename, by calling fopen(filename, strmode). 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<Elem, 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.

Example

See basic_filebuf::close for an example that uses open.

Requirements

Header: <fstream>

Namespace: std

See Also

Reference

basic_filebuf Class

iostream Programming

iostreams Conventions

Other Resources

basic_filebuf Members