如何在Qt中获取文件大小? [英] How to get the file size in Qt?

查看:3720
本文介绍了如何在Qt中获取文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Qt中获取文件大小?



i使用了GetFileSize(),但它不起作用..请帮助我..

How to get the file size in Qt??

i have used GetFileSize() but its not working.. pls help me..

推荐答案

你可以简单地使用QFile :: size()函数来获取文件大小。
You can simply use QFile::size() function to get size of file.






有一个关于Qt项目论坛中的这个主题。

你的情况可能出现的错误可能是,如果无法打开文件进行读取,QFile :: size()会返回'0',即使文件也是如此大小大于'0'。因此,您需要检查是否可以打开文件进行读取操作(之后不要忘记关闭它!):



Hi,

there is a thread about this topic in the Qt Project forum.
A possible error in your case may be that QFile::size() returns '0' if the file can't be opened for read, even if the file size is greater than '0'. Therefore you need to check whether the file can be opened for a read operation (And don't forget to close it afterwards!):

int size = 0;
QFile myFile("C:\\file.txt");
if (myFile.open(QIODevice::ReadOnly)){
    size = myFile.size();  //when file does open.
    myFile.close();
} 





来源: Qt项目论坛 [ ^ ] < br $>




欢呼

mabertschi



Source: Qt Project Forums[^]


cheers
mabertschi


这篇关于如何在Qt中获取文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆