PDA

View Full Version : QFile open question



chenxuelian
26th March 2010, 08:51
hi, everyone,
i have a question to ask you. it's about QFile. my code is:

QFile file("/proc/mounts");
if (!file.exists())
{
qDebug() << "exist error" << file.error();
file.close();
return 0;
}

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "open error" << file.error();
file.close();
return 0;
}
.......
file.close();
when i open the file some times, and then it printf "open error 4" ,4 means QFile::ResourceError,what's the problem.
thanks!

Kumosan
26th March 2010, 12:48
There might not be a problem at all. "/proc/mounts" is not a real file. It is a virtual file, which is created by Linux itself. Generally you can open it and treat it as a normal file, however, some functions do not work or do not work properly, e.g. it might not be possible to get the correct size or determine the end of the file. Though I don't think this applies to opening it. Perhaps you reached your max open file limit? In this case your problem is independent of "/proc/mounts" and would explain the resource error. Do you have plenty of open files? And remember, almost everything in unix is a file.

chenxuelian
29th March 2010, 08:53
thanks for your answer.I just open this one file.I aim to detect U flash disk when has U flash.because /proc/mounts can tell us the message about the U flash, I open the file to get the message.

Kumosan
29th March 2010, 15:12
Hard to understand you.
So you open /proc/mounts to test whether or not your flash disk is mounted or not? Should work, but is not platform independent. But this was not the question.

Don't make the mistake to assume that just because you open just one file, it is the only file opened in your system. If other programs, e.g. p2p-programs, keeps a lot of files open, your kernel might out of file handles. This might explain why it works sometimes and sometimes not. As I said, I am not sure, but QFile::ResourceError means probable 'too many open files' <-- google for it.

Lykurg
29th March 2010, 15:44
Just a quick note: Works well on my Kubuntu. So no "general" problem.
#include <QtGui>

int main(int argc, char** argv)
{
QFile file("/proc/mounts");
if (!file.exists())
{
qWarning() << "exist error" << file.error();
file.close();
return 0;
}
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "open error" << file.error();
file.close();
return 0;
}
qWarning() << file.readAll();
file.close();
return 0;
}

Kumosan
29th March 2010, 16:49
So no "general" problem.

Yep, that's why I think he might be close to some limit. 'Too many open files' would explain nicely why it sometimes work and sometimes not.

chenxuelian
1st April 2010, 04:53
'Too many open files' ? then how can I do it?
now , I have another problem. I think it's the same proble. I use QDir::entryInfoList to get the fileinfo, but I can get the info sometimes but sometimes not.

drhex
1st April 2010, 11:24
Does it work better if you open /etc/mtab instead?

chenxuelian
2nd April 2010, 06:58
no ,it's the same problem

Kumosan
2nd April 2010, 11:00
'Too many open files' ? then how can I do it?
now , I have another problem. I think it's the same proble. I use QDir::entryInfoList to get the fileinfo, but I can get the info sometimes but sometimes not.

If you use Linux look into /etc/security/limits. There you can tweak the max files on a per user base. For more information:
http://www.karakas-online.de/forum/viewtopic.php?t=9834
Works like a charm.