Results 1 to 10 of 10

Thread: QFile open question

  1. #1
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QFile open question

    hi, everyone,
    i have a question to ask you. it's about QFile. my code is:
    Qt Code:
    1. QFile file("/proc/mounts");
    2. if (!file.exists())
    3. {
    4. qDebug() << "exist error" << file.error();
    5. file.close();
    6. return 0;
    7. }
    8.  
    9. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    10. {
    11. qDebug() << "open error" << file.error();
    12. file.close();
    13. return 0;
    14. }
    15. .......
    16. file.close();
    To copy to clipboard, switch view to plain text mode 
    when i open the file some times, and then it printf "open error 4" ,4 means QFile::ResourceError,what's the problem.
    thanks!
    Last edited by wysota; 27th March 2010 at 14:02. Reason: Missing [code] tags

  2. #2
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QFile open question

    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile open question

    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.

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QFile open question

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QFile open question

    Just a quick note: Works well on my Kubuntu. So no "general" problem.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char** argv)
    4. {
    5. QFile file("/proc/mounts");
    6. if (!file.exists())
    7. {
    8. qWarning() << "exist error" << file.error();
    9. file.close();
    10. return 0;
    11. }
    12. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    13. {
    14. qWarning() << "open error" << file.error();
    15. file.close();
    16. return 0;
    17. }
    18. qWarning() << file.readAll();
    19. file.close();
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QFile open question

    Quote Originally Posted by Lykurg View Post
    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.

  7. The following user says thank you to Kumosan for this useful post:

    chenxuelian (2nd April 2010)

  8. #7
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile open question

    '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.

  9. #8
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QFile open question

    Does it work better if you open /etc/mtab instead?

  10. #9
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile open question

    no ,it's the same problem

  11. #10
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QFile open question

    Quote Originally Posted by chenxuelian View Post
    '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.

Similar Threads

  1. QFile::open throws std::bad_alloc for no obvious reason
    By nateriver in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2009, 06:10
  2. possible open/write two files same time with QFile
    By npotency in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2009, 00:28
  3. Qfile question
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2008, 15:32
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58
  5. QFile::writeBlock: File not open
    By safknw in forum Qt Tools
    Replies: 1
    Last Post: 16th September 2006, 13:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.