Qt/C++如何定位在文件的中间(偏移)->唯一知道的是文件大小
大家好,在 Qt 中我有:
FILE *pInFile = fopen(strFileName.toLatin1().constData(), "r");
QFileInfo fi(strFileName);
qint64 fileSize = fi.size();
//GO TO THE MIDDLE
//WHAT IS THE POSITION OF THE MIDDLE (INTEGER)
我想知道文件中间的偏移量,而不是在循环中读取整个文件(fgets)。基于该偏移量,我想获得该位置。
基本上;
- 根据给定的字节(例如:fileSize/2)从文件中获取偏移量
- 基于该偏移量的位置是什么(行索引)
是否可以使用类似的东西来确定位置? int centerPos = ftell(偏移中间, pInFile);
我认为我走的路不对,你能给我一些建议吗?
谢谢
。
并且最好定位在该位置的开头
Hy all, in Qt i have:
FILE *pInFile = fopen(strFileName.toLatin1().constData(), "r");
QFileInfo fi(strFileName);
qint64 fileSize = fi.size();
//GO TO THE MIDDLE
//WHAT IS THE POSITION OF THE MIDDLE (INTEGER)
Instead of reading trough the whole file (fgets) in a loop, i want to know the offset of the middle of the file. And based on that offset i want to get that position.
Basically;
- get offset from a file based on my given bytes (example: fileSize/2)
- based on that offset what is the position (row index)
Is it possible to have something like this for determining position?
int centerPos = ftell(Offset middle, pInFile);
I don't think i'm on the right path here, can u give some advice?
Thx
ps.
And would be nice to position on the the beginning of the position
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 QFile 而不是原始文件指针。它有一个
seek
方法。http://doc.qt.nokia.com/latest/qfile.html#seek
您可以使用...确定文件的中间位置
,然后,如上所述,在调用后从该位置开始读取
Use
QFile
instead of a raw file pointer. It has aseek
method.http://doc.qt.nokia.com/latest/qfile.html#seek
You can determine the middle of the file using
...then, as mentioned, start reading at that position after calling
然后定义以下函数
void middle_fseek(FP* FilePointer, int offset)
{
fseek(FilePointer, middle+ offset, SEEK_SET);
}
Then define the following function
void middle_fseek(FP* FilePointer, int offset)
{
fseek(FilePointer, middle+ offset, SEEK_SET);
}