輸入資料流成員函式

輸入資料流成員函式來偵測磁碟的輸入。成員函式包括:

  • 開啟函數的輸入資料流

  • Get 函式

  • Getline 函式

  • 讀取函式

  • Seekg 和 tellg 函式

  • 輸入資料流關閉的功能

開啟函數的輸入資料流

如果您正在使用的輸入的檔資料流 (ifstream),必須將此資料流中與特定的磁碟檔案產生關聯。您可以在建構函式,或者您也可以使用開啟函式。不論是哪一種情況中,引數是相同的。

通常指定 ios_base::openmode 旗標,當您開啟的輸入資料流相關聯的檔案 (預設模式是 ios::in)。為一系列 open_mode 旗標,請參閱開啟的函式。可以與位元 OR 運算結合旗標 (|) 運算子。

若要讀取的檔案,請先使用失敗成員函式,以判斷它是否存在:

istream ifile( "FILENAME" );
if ( ifile.fail() )
// The file does not exist ...

Get 函式

未格式化取得 成員函式的作用類似 >> 運算子有兩個例外。第一, 取得 函式包含空格字元,而抽選程式 」 不包含泛空白字元,則當 skipws 旗標設定 (預設值)。第二個, 取得函式不太可能會造成採用的輸出資料流 (cout,例如) 清除。

一種取得函式指定的緩衝區位址,以及要讀取的字元數目上限。這是適用於限制的字元傳送給特定的變數,如這個範例所示:

// ioo_get_function.cpp
// compile with: /EHsc
// Type up to 24 characters and a terminating character. 
// Any remaining characters can be extracted later.
#include <iostream>
using namespace std;

int main()
{
   char line[25];
   cout << " Type a line terminated by carriage return\n>";
   cin.get( line, 25 );
   cout << line << endl;
}

f5tsy854.collapse_all(zh-tw,VS.110).gif輸入

1234

f5tsy854.collapse_all(zh-tw,VS.110).gif範例輸出

1234

Getline 函式

Getline 成員函式會類似於取得函式。這兩個功能讓某些第三個引數指定輸入結束的字元。預設值是新行字元。這兩個函式會保留一個必要的終止字元的字元。不過, 取得 離開終止字元資料流中, getline 移除結束的字元。

下列範例會指定輸入資料流的終止字元:

// getline_func.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main( )
{
   char line[100];
   cout << " Type a line terminated by 't'" << endl;
   cin.getline( line, 100, 't' );
   cout << line;
}

f5tsy854.collapse_all(zh-tw,VS.110).gif輸入

test

讀取函式

讀取成員函式從檔案讀取的位元組,以指定的記憶體區域。長度的引數會決定讀取的位元組數目。如果您未包含該引數,讀取停止達到實體檔案結尾或,如果是文字模式的檔案,當內嵌EOF在讀取字元。

本範例從薪資檔案讀取二進位記錄成結構:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
   struct
   {
      double salary;
      char name[23];
   } employee;

   ifstream is( "payroll" );
   if( is ) {  // ios::operator void*()
      is.read( (char *) &employee, sizeof( employee ) );
      cout << employee.name << ' ' << employee.salary << endl;
   }
   else {
      cout << "ERROR: Cannot open file 'payroll'." << endl;
   }
}

程式假設資料記錄的格式與指定的相同結構與未結束的歸位或換行字元。

Seekg 和 tellg 函式

輸入的檔案資料流保留的內部指標,以在接下來讀取資料的檔案中的位置。您將設定使用 this 指標seekg函式,如下所示:

#include <iostream>
#include <fstream>
using namespace std;

int main( )
{
   char ch;

   ifstream tfile( "payroll" );
   if( tfile ) {
      tfile.seekg( 8 );        // Seek 8 bytes in (past salary)
      while ( tfile.good() ) { // EOF or failure stops the reading
         tfile.get( ch );
         if( !ch ) break;      // quit on null
         cout << ch;
      }
   }
   else {
      cout << "ERROR: Cannot open file 'payroll'." << endl;
   }
}

若要使用seekg可實作資料錄為導向的資料管理系統,將固定長度記錄取得的位元組位置相對的檔案結尾,然後使用該資料錄數字取得讀取資料錄的物件。

tellg成員函式會傳回用於讀取目前的檔案位置。這個值屬於型別streampos、 typedef <iostream> 所述。下列範例會讀取檔案,並顯示訊息的顯示空間的位置。

#include <fstream>
#include <iostream>
using namespace std;

int main( )
{
   char ch;
   ifstream tfile( "payroll" );
   if( tfile ) {
       while ( tfile.good( ) ) {
          streampos here = tfile.tellg();
          tfile.get( ch );
          if ( ch == ' ' )
             cout << "\nPosition " << here << " is a space";
       }
   }
   else {
      cout << "ERROR: Cannot open file 'payroll'." << endl;
   }
}

輸入資料流關閉的功能

關閉成員函式會關閉輸入的檔案資料流相關聯的磁碟檔案,並釋放作業系統的檔案控制代碼。Ifstream 解構函式會關閉檔案,但是您可以使用關閉函式,如果您需要開啟相同的資料流物件的另一個檔案。

請參閱

參考

輸入資料流