C++ seekp和seekg函数用法详解
发布时间:2020-12-16 07:37:37 所属栏目:百科 来源:网络整理
导读:文件流对象有两个成员函数,分别是 seekp 和 seekg 。它们可以用于将读写位置移动到文件中的任何字节。 seekp 函数用于已经打开要进行输出的文件,而 seekg 函数则用于已经打开要进行输入的文件。可以将 "p" 理解为 "put",将 "g" 理解为 "get",这样理解当然
|
文件流对象有两个成员函数,分别是 seekp 和 seekg。它们可以用于将读写位置移动到文件中的任何字节。 seekp 函数用于已经打开要进行输出的文件,而 seekg 函数则用于已经打开要进行输入的文件。可以将 "p" 理解为 "put",将 "g" 理解为 "get",这样理解当然是有根据的,因为 seekp 可用于将信息 put(放入)到文件中,而 seekg 则可用于从文件中 get(获取)信息。 以下是 seekp 的用法示例: file.seekp(20L,ios::beg); 第一个实参是一个 long 类型的整数,表示文件中的偏移量。这就是想要移动到的字节数。在该示例中,使用的是 20L(请记住,L 字符可以强制编译器将该数字视为一个 long 类型的整数)。该语句可以将文件的写入位置移动到编号为 20 的字节(所有编号从 0 开始,因此编号为 20 的字节实际上是第 21 个字节)。第二个实参称为模式标志,它指定从哪里计算偏移量。标志 ios::beg 表示偏移量是从文件开头算起的。也可以修改该参数,从文件末尾或文件中的当前位置计算偏移量。表 1 列出了所有 3 种随机访问模式的标志。
表 2 显示了 seekp 和 seekg 使用不同模式标志的示例。
请注意,表 2 中的一些示例使用了负偏移量。负偏移量导致读或写位置在文件中向后移动,而正偏移量则导致向前移动。 假设文件 letters.txt 中包含以下数据:
Byte 5 from beginning: f
//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
// 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),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: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby-on-rails – Rails捕获所有路由阻止访问公用文件夹
- objective-c – 在NSCache中使用NSPurgeabledata有什么意义
- ajax – MarkLogic HTTP Server:设置CORS头
- flash – 对网页上的嵌入对象使用wmode =“transparent”,“
- Tokyo Cabinet和Tokyo Tyrant简介
- 内部类反序列化问题(fastjson exception: create instance
- Match One of Two Alternatives Based on a Condition (基于
- 在Flex中实现聊天表情图片支持-资料篇
- sqlite使用方法整理
- “非零退出状态”R 3.0.1“XML”和“RCurl”
