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
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有