file.seekp(20L, ios::beg);
第一個實參是一個 long 型別的整數,表示檔案中的偏移量。這就是想要移動到的位元組數。在該範例中,使用的是 20L(請記住,L 字元可以強制編譯器將該數位視為一個 long 型別的整數)。該語句可以將檔案的寫入位置移動到編號為 20 的位元組(所有編號從 0 開始,因此編號為 20 的位元組實際上是第 21 個位元組)。模式標誌 | 描 述 |
---|---|
ios::beg | 從檔案頭開始計算偏移量 |
ios::end | 從檔案末尾開始計算偏移量 |
ios::cur | 從當前位置開始計算偏移量 |
語 句 | 如何影響讀/寫位置 |
---|---|
file.seekp(32L, ios::beg); | 將寫入位置設定為從檔案開頭開始的第 33 個位元組(位元組 32) |
file.seekp(-10L, ios::end); | 將寫入位置設定為從檔案末尾開始的第 11 個位元組(位元組 10) |
file.seekp(120L, ios::cur); | 將寫入位置設定為從當前位置開始的第 121 個位元組(位元組 120) |
file.seekg(2L, ios::beg); | 將讀取位置設定為從檔案開頭開始的第 3 個位元組(位元組 2) |
file.seekg(-100L, ios::end); | 將讀取位置設定為從檔案末尾開始的第 101 個位元組(位元組 100) |
file.seekg(40L, ios::cur); | 將讀取位置設定為從當前位置開始的第 41 個位元組(位元組 40) |
file.seekg(0L, ios:rend); | 將讀取位置設定為檔案末尾 |
Byte 5 from beginning: f
Byte 10 from end: q
Byte 3 from current: u
//This program demonstrates the seekg function. #include <iostream> #include <fstream> using namespace std; int main() { // Variable to access file char ch; // Open the file for reading fstream file ("letters.txt", ios::in); if (!file) { cout << "Error opening file."; return 0; } // Get fifth byte from beginning of alphabet file file.seekg(5L, ios::beg); file.get(ch); cout << "Byte 5 from beginning: " << ch << endl; // Get tenth byte from end of alphabet file file.seekg(-10L, ios::end); file.get(ch); cout << "Byte 10 from end: " << ch << endl; //Go forward three bytes from current position file.seekg(3L, ios::cur); file.get(ch); cout << "Byte 3 from current: " << ch << endl; // Close file file.close (); return 0; }程式輸出結果:
Byte 5 from beginning: f
Byte 10 from end: q
Byte 3 from current: u
// This program demonstrates the use of a structure // variable to read a record of information from a file. #include <iostream> #include <fstream> using namespace std; const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14; //宣告記錄的結構 struct Info { char name[NAME_SIZE]; int age; char address1[ADDR_SIZE]; char address2[ADDR_SIZE]; char phone[PHONE_SIZE]; }; // Function Prototypes long byteNum(int); void showRec(Info); int main() { // Person information Info person; // Create file object and open the file fstream people("people.dat", ios::in | ios::binary); if (!people) { cout << "Error opening file. Program aborting.n"; return 0; } // Skip forward and read record 1 in the file cout << "Here is record 1:n"; people.seekg(byteNum(1), ios::beg); people.read(reinterpret_cast<char *>(&person), sizeof (person)); showRec(person); // Skip backwards and read record 0 in the file cout << "nHere is record 0:n"; people.seekg(byteNum(0), ios::beg); people.read(reinterpret_cast<char *>(&person), sizeof (person)); showRec(person); // Close the file people.close(); return 0; } long byteNum(int recNum) { return sizeof (Info) * recNum; } void showRec(Info record) { cout << "Name:"; cout << record.name << endl; cout << "Age: "; cout << record.age << endl; cout << "Address line 1: "; cout << record.address1 << endl; cout << "Address line 2: "; cout << record.address2 << endl; cout << "Phone: "; cout << record.phone << endl; }程式螢幕輸出結果:
Here is record 1:
Name:cyuyan
Age: 20
Address line 1: No.1
Address line 2: No.2
Phone: 12345678
Here is record 0:
Name:http://c.biancheng.net
Age: 5
Address line 1: No.1
Address line 2: No.2
Phone: 123456