一.标准c++文件流操作方式:
对输入流操作
seekg():设置输入文件流的文件流指针位置
tellg():返回当前定位指针的位置,也代表着输入流的大小。
对输出流操作
seekp():设置输出文件流的文件流指针位置
tellp():用于输入流,返回流中‘get’指针当前的位置
函数原型:
ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
istream& seekg( streampos pos );
istream& seekg( streamoff off, ios::seek_dir dir );
函数参数:
pos:新的文件流指针位置值,可以是正负数值,正的表示向后偏移,负的表示向前偏移。
off:需要偏移的值
dir:搜索的起始位置
dir参数用于对文件流指针的定位操作上,代表搜索的起始位置。在ios中定义的枚举类型: enum seek_dir {beg, cur, end};每个枚举常量的含义:
ios::beg:文件流的起始位置
ios::cur:文件流的当前位置
I os::end:文件流的结束位置
调用示例:
表 2 文件寻找操作 | |
语 句 | 如何影响读/写位置 |
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); | 将读取位置设置为文件末尾 |
程序示例:
假设文件 letters.txt 中包含以下数据:
Byte 5 from beginning: f
Byte 10 from end: q
Byte 3 from current: u
下面的程序使用了 seekg 函数跳转到 letters.txt 文件中的不同位置,每次停止后都检索一个字符:
- //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
下面的程序显示了 seekg 函数的另一个例子。它打开了包含两个记录的 people.dat 文件。该程序首先显示记录 1(第二条记录),然后显示记录 0。
- // 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
该程序除了 main 以外还有两个重要的函数。第一个是 byteNum,它釆用一个记录编号作为实参,并返回该记录的起始字节。它通过将记录编号乘以 Info 结构的大小来计算记录的起始字节。这将从文件的开始处返回该记录的偏移量。第二个函数是 showRec,它接收一个 Info 结构作为实参,并在屏幕上显示其内容。
二. MFC CFile类调用
virtual ULONGLONG Seek( LONGLONG lOff, UINT nFrom );
参数:
lOff 移動檔案指標的位元組數目。 正值會向上捲動到檔案結尾的檔案指標;負值會將會向檔案的起始檔案指標。
nFrom 要尋找的位置。 的可能值。
傳回值資料指標的位置,如果方法成功,否則,傳回值為未定義,並為 CFileException 例外狀況的指標會擲回。
备注:
下表列出 nFrom 參數的可能值。
CFile::begin 搜尋檔案開頭。
CFile::current 從檔案指標的目前位置的搜尋。
CFile::end 從檔案結尾的搜尋。
在開啟檔案時,檔案指標放置在 0 中,檔案的開頭。 您可以將檔案指標至超出檔案尾端的位置。 如果您這樣做,檔案大小不增加,直到寫入檔案。 在例外狀況處理後,這個方法的例外處理常式必須刪除例外狀況物件。
实例:
CFile cfile;
cfile.Open(_T("Seek_File.dat"), CFile::modeCreate | CFile::modeReadWrite);
LONGLONG lOffset = 1000;
ULONGLONG lActual;
lActual = cfile.Seek(lOffset, CFile::begin);
三 C标准库函数fseek()
C 库函数 int fseek(FILE *stream, long int offset, int whence) 设置流 stream 的文件位置为给定的偏移 offset,参数 offset 意味着从给定的 whence 位置查找的字节数。
声明
下面是 fseek() 函数的声明。
int fseek(FILE *stream, long int offset, int whence)
参数
- stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
- offset -- 这是相对 whence 的偏移量,以字节为单位。
- whence -- 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一:
常量 | 描述 |
SEEK_SET | 文件的开头 |
SEEK_CUR | 文件指针的当前位置 |
SEEK_END | 文件的末尾 |
返回值
如果成功,则该函数返回零,否则返回非零值。
实例
下面的实例演示了 fseek() 函数的用法。
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt","w+");
fputs("This is runoob.com", Fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming Langauge", fp);
fclose(fp);
return(0);
}