Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
230
|
第
13
章
13.5 文件随机访问
文件随机访问是指在某个文件内直接读写任何给定位置数据的能力。通过获取与设定文
件位置指示符可以实现这一功能,文件位置指示符指定了文件中的当前访问位置,该文
件与一个给定的流关联。
13.5.1 获取当前文件位置
下面的函数返回当前文件的访问位置。当需要标记文件中的位置,以便以后返回到该位
置时,可以使用下面的函数。
long ftell( FILE *
fp
);
ftell()
返回
fp
流的文件位置。对一个二进制流来说,它与该位置之前的字符数量
是相同的,也就是当前字符位置距离文件头部的偏差。当发生错误时,
ftell()
返回
-
1
。
int fgetpos(FILE * restrict
fp
,
fpos_t * restrict
ppos
)
;
fgetpos()
将
fp
流的文件位置指示符写入
ppos
所引用的对象,该对象类型为
fpos_
t
。如果
fp
是一个宽字符导向流,那么
fgetpos()
所存储的指示符也会包含流当前的
转换状态(请参见
13.4.1
节)。当发生错误时,
fgetpos()
返
回非
0
值;当执行成功
时,返回
0
。
下面的示例记录文件
messages.txt
中以
#
字符开头的所有行的位置:
#define ARRAY_LEN 1000
long arrPos[ARRAY_LEN] = { 0L };
FILE *fp = fopen( "messages.txt", "r" );
if ( fp != NULL)
{
int i = 0, c1 = '\n', c2;
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207