分享方式:


輸入資料流成員函式

輸入資料流成員函式是用於磁碟輸入。

open

如果您使用輸入檔案數據流 (ifstream),則必須將該數據流與特定磁碟檔案產生關聯。 您可以在建構函式中執行此動作,或使用 函 open 式。 不論是上述哪一種情況,引數都相同。

當您開啟與輸入數據流相關聯的檔案時,通常會指定 ios_base::openmode 旗標(預設模式為 ios::in)。 如需旗標的清單 openmode ,請參閱 ios_base::openmode。 旗標可以與位 「or」 (|) 運算子結合。

若要讀取檔案,請先使用 fail 成員函式來判斷它是否存在:

istream ifile("FILENAME");

if (ifile.fail())
// The file does not exist ...

get

未格式化 get 的成員函式的運作方式類似運算元, >> 但有兩個例外狀況。 首先,函 get 式包含空格符,而擷取器會在設定旗標時 skipws 排除空格符(預設值)。 其次,函 get 式不太可能造成系結的輸出數據流 (cout例如, 例如) 排清。

函式的變化 get 會指定要讀取的緩衝區位址和最大字元數。 這對於限制傳送給特定變數的字元數來說,相當有用,如以下範例所示:

// 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;
}

輸入

1234

範例輸出

1234

getline

成員 getline 函式類似於函式 get 。 這兩個函式都允許使用第三引數來指定輸入的終止字元。 預設值是新行字元。 這兩個函式都會保留一個字元作為所需的終止字元。 不過, get 離開數據流中的終止字元,並 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;
}

輸入

test

read

成員 read 函式會將位元組從檔案讀取到指定的記憶體區域。 長度引數會決定所讀取的位元組數目。 如果您沒有包含該自變數,讀取會在到達檔案的實體結尾時停止,或在讀取內嵌 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;
   }
}

程式假設數據記錄的格式完全與結構所指定,且沒有終止歸位字元或換行字元。

seekgtellg

輸入檔案資料流會保留內部指標,該指標指向檔案中接下來要讀取資料的位置。 您可以使用 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 來實作記錄導向的數據管理系統,請將固定長度的記錄大小乘以記錄編號,以取得相對於檔案結尾的位元組位置,然後使用 get 物件來讀取記錄。

tellg 成員函式會傳回目前的檔案位置以供讀取。 這個值的類型為 streampostypedef 定義於 中 <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;
   }
}

close

成員 close 函式會關閉與輸入檔案數據流相關聯的磁碟檔案,並釋放操作系統檔句柄。 ifstream解構函式會為您關閉檔案,但如果您需要開啟相同數據流物件的另一個檔案,您可以使用 函close式。

另請參閱

輸入資料流