Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

10.3 文件

通常,我们需要处理的数据会多得难以放入计算机的内存之中,因此我们将大部分数据存放于磁盘或其他大容量存储设备中。这种设备还具有另外一个我们需要的特性:断电后,保存在其中的数据不会丢失——即数据是持久的。究其根本,一个文件可以简单看作一个从0开始编号的字节序列。

每个文件都有自己的格式,也就是说,有一组规则来确定其中字节的含义。例如,如果是一个文本文件,前4个字节就是内容中的前4个字符。如果是一个二进制表示的整数文件,同样是前4个字节,表示的就是第1个整数了(参见11.3.2节)。格式之于磁盘文件的作用,与类型之于内存对象的作用是一样的。当(且仅当)我们知道一个文件的格式(参见11.2.3节),我们就能弄清文件中比特流的含义了。

对于一个文件,ostream将内存中的对象转换为字节流,再将字节流写到磁盘上。istream进行相反的操作,也就是说,它从磁盘获取字节流,将其转换为对象。

多数情况下,我们假定这些“磁盘上的字节”都是常用字符集中的字符。事实并不总是如此,但绝大多数情况下我们可以认为这个假定是对的,而且,其他的字符集表示方式也是不难处理的。我们还假定所有文件都保存在磁盘上(也就是说,保存在旋转磁存储设备上)。同样,这个假设也不总是成立(考虑闪存设备),但在编程的层面上,实际采用哪种存储设备是没什么区别的,这也是文件和流抽象层的好处之一。 ...

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

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252