共用方式為


basic_filebuf::close

關閉檔案。

basic_filebuf<Elem, Tr> *close( );

傳回值

如果檔案指標為 null 指標,成員函式傳回 null 指標。

備註

close 呼叫 fclose(fp)。 如果該函式傳回非零的值,則函式傳回 null 指標。 否則,會傳回 this 表示檔案已成功關閉。

若為寬資料流,則為,如果任何插入之後,就會開啟資料流,或從上次呼叫 streampos後,函式呼叫 溢位。 它也插入必要的任何序列還原初始的轉換狀態,使用檔案轉換 Facet fac 呼叫 fac.unshift 視需要。 型別 char 的每個項目而產生的 byte 給檔案指標指定的關聯資料流寫入 fp ,好像由表單 fputc(byte), fp的後續呼叫。 如果對 fac.unshift 的或任何寫入失效,函式失敗。

範例

下列範例假設在目前目錄中的兩個檔案:basic_filebuf_close.txt (Content 「test」) 和 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 類別

iostream 程式設計

iostreams 慣例