basic_filebuf::close

关闭文件。

basic_filebuf<Elem, Tr> *close( );

返回值

文件,则指针为 null 指针,成员函数返回null指针。

备注

关闭 调用 fclose(fp)。如果该函数返回非零值,则函数将返回null指针。否则,它将返回 this 清单文件成功已关闭。

为宽流,因此,如果任何插入发生,且在打开了流,或者,因为最后一次调用 streampos,函数调用 溢出。它还插入必要的任何顺序还原初始转换状态,使用文件转换方面 fac 调用 fac.unshift 根据需要。类型 char 的每个元素而生成的 byte 将文件指针指定的关联流中写入 fp,就象由连续调用该窗体 fputc(bytefp)。如果对 fac.unshift 或任何编写的调用失败,该函数不成功。

示例

下面的示例假定在当前目录中的两个文件:basic_filebuf_close.txt (目录“测试”)和iotest.txt (内容为“ssss”)。

// basic_filebuf_close.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>

int main() {
   using namespace std;
   ifstream file;
   basic_ifstream <wchar_t> wfile;
   char c;
   // Open and close with a basic_filebuf
   file.rdbuf()->open( "basic_filebuf_close.txt", ios::in );
   file >> c;
   cout << c << endl;
   file.rdbuf( )->close( );

   // Open/close directly
   file.open( "iotest.txt" );
   file >> c;
   cout << c << endl;
   file.close( );

   // open a file with a wide character name
   wfile.open( L"iotest.txt" );

   // Open and close a nonexistent with a basic_filebuf
   file.rdbuf()->open( "ziotest.txt", ios::in );
   cout << file.fail() << endl;
   file.rdbuf( )->close( );

   // Open/close directly
   file.open( "ziotest.txt" );
   cout << file.fail() << endl;
   file.close( );
}
  

要求

标头: <fstream>

命名空间: std

请参见

参考

basic_filebuf Class

iostream编程

(mfc)约定