C++ 数据序列化与文件处理实战
在 C++ 编程中,数据序列化和文件处理是非常重要的部分。本文将详细介绍如何使用不同的库进行 XML、JSON 数据的序列化和反序列化,以及如何创建包含图像的 PDF 文件和处理 ZIP 归档文件。
1. XML 数据的序列化和反序列化
在 C++ 标准库中,没有对 XML 的支持,但有很多开源的跨平台库可以使用,例如 Xerces - C++、libxml++、tinyxml 或 tinyxml2、pugixml、gSOAP 和 RapidXml 等。这里我们选择 pugixml 库,它是一个跨平台、轻量级的库,具有快速的 XML 解析器,虽然不支持模式验证,但有类似 DOM 的接口,支持 Unicode 和 XPath 1.0。
1.1 数据结构定义
为了表示电影数据,我们定义以下结构体:
struct casting_role { std::string actor; std::string role; }; struct movie { unsigned int id; std::string title; unsigned int year; unsigned int length; std::vector<casting_role> cast; std::vector<std::string> directors; std::vecto