简体   繁体   English

在Qt中读写二进制文件

[英]Reading and writing binary files in Qt

I'm going to be working with binary files in a Qt project, and, being a little new to Qt, am not sure whether or not I should use a QVector<quint8> or a QByteArray to store the data. 我将在Qt项目中使用二进制文件,并且对Qt来说有点新,我不确定是否应该使用QVector<quint8>QByteArray来存储数据。 The files could be very small (< 1MiB) or very large (> 4GiB). 文件可能非常小(<1MiB)或非常大(> 4GiB)。 The size is unknown until runtime. 在运行时之前,大小是未知的。

I need to be able to have random seek and be able to process operations on every byte in the file. 我需要能够进行随机搜索,并能够处理文件中每个字节的操作。 Would a memory mapped file be of any use here? 内存映射文件在这里有用吗?

Thank you for any suggestions. 谢谢你的任何建议。

Loading whole large files in memory, be it QVector or QByteArray is probably not a good solution. 将整个大文件加载到内存中,无论是QVector还是QByteArray都可能不是一个好的解决方案。

Assuming the files have some kind of structure, you should use QFile::seek to position yourself at the start of a "record" and use qint64 QIODevice::read ( char * data, qint64 maxSize ) to read one record at a time in a buffer of your choice. 假设文件有某种结构,你应该使用QFile::seek将自己定位在“记录”的开头,并使用qint64 QIODevice::read ( char * data, qint64 maxSize )读取一条记录你选择的缓冲区。

QIODevice::write has an overload for QByteArray if that affects your decision. QIODevice::writeQByteArray有重载,如果这会影响您的决定。 QDataStream may be worth looking at for larger data. QDataStream可能值得查看更大的数据。 At the end of the day it's really up to you as various containers would work. 在一天结束时,由于各种容器都可以工作,所以由您自己决定。

Edit: 编辑:

I think basic file I/O using whatever buffer you prefer is probably all you need. 我认为使用你喜欢的缓冲区的基本文件I / O可能就是你所需要的。 Use objects like QFile , QDataStream , QByteArray , etc. You could read and process only parts of the file with circular buffers to save memory especially if dealing with audio, video, or something that lends itself to streams. 使用QFileQDataStreamQByteArray等对象。您可以使用循环缓冲区仅读取和处理文件的某些部分以节省内存,尤其是在处理音频,视频或适合流的内容时。 If there is a known structure to the file like XML, CSV, etc that also makes partial reads and processing easier as you can go line by line or tag by tag. 如果文件的已知结构(如XML,CSV等)也可以使部分读取和处理更容易,因为您可以逐行或逐个标记。

Memory mapped files use virtual memory to achieve faster I/O by basically making a copy of a file on disk in a virtual memory segment which is then capable of being used by the application as if it was just process memory. 内存映射文件使用虚拟内存来实现更快的I / O,基本上是在虚拟内存段中的磁盘上创建一个文件的副本,然后该应用程序可以将其用作应用程序,就好像它只是进程内存一样。 Being able to treat a file as process memory allows you to do in place editing which is faster than having to seek to a position from the beginning of a file and faster than making OS dependent API calls and dealing with hard disk read/writes. 能够将文件视为进程内存允许您进行就地编辑,这比从文件开头寻找位置更快,而且比使用OS依赖的API调用和处理硬盘读/写更快。 There does tend to be a fair amount of overhead to memory mapped files and there are some possible limitations depending on how paging is implemented in your target platform or what architecture you're using. 内存映射文件确实存在相当大的开销,并且存在一些可能的限制,具体取决于在目标平台中实现分页的方式或您正在使用的体系结构。 In Qt you would have to design your own objects to use memory mapped files and historically I believe linux systems support this functionality better than windows. 在Qt中你必须设计自己的对象来使用内存映射文件,而且从历史上看,我相信linux系统比windows更好地支持这个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM