Results 1 to 2 of 2

Thread: Problem Regarding QFile....!

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Problem Regarding QFile....!

    hi i am trying to copy data using QFile from multiple files to single file, i.e file A and file B data should be stored in file C
    here is the sample code that i am using,
    once please check the code below:
    Qt Code:
    1. QFile readfile("/mnt/sysuser/Analog_srno");
    2. if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text))
    3. qDebug("file read file not open");
    4.  
    5. QFile dest("/mnt/sysuser/CardTest");
    6. if(!dest.open(QIODevice::Append | QIODevice::Text))
    7. qDebug("file dest file not open");
    8.  
    9. if(readfile.readAll().isEmpty())
    10. {
    11. Enter_Cpuno();
    12. }
    13. else
    14. {
    15. QByteArray buffer;
    16. qDebug("entering into while");
    17. while(!(buffer=readfile.readAll()).isEmpty())
    18. {
    19. write.dest(buffer);
    20. }
    21. readfile.close();
    22. }
    23. QFile read_file("/mnt/sysuser/Cpu_srno");
    24. if(!read_file.open(QIODevice::ReadOnly | QIODevice::Text))
    25. qDebug("file read file not open");
    26.  
    27. if(read_file.readAll().isEmpty())
    28. {
    29. Enter_Cpuno1();
    30. }
    31.  
    32. else
    33. {
    34. QByteArray buffer;
    35. qDebug("entering into while");
    36. while(!(buffer=readfile.readAll()).isEmpty())
    37. {
    38. write.dest(buffer);
    39. }
    40. read_file.close();
    41. dest.close();
    42. }
    To copy to clipboard, switch view to plain text mode 
    I am using the version of QT-4.7.4 -Please notify me if there is an error ,
    Please help/guide me,
    thanks in advance,
    rohith.g
    Last edited by anda_skoa; 26th November 2014 at 06:04. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem Regarding QFile....!

    You call readAll() in your if() conditions. That reads all data from the file.

    Assuming there was data, you enter the else part of your code, which then calls readAll() again.
    Since the file is at the end already, the result is an empty byte array.

    Better use QFile::size() to check if the file is not empty.

    And you don't need to specify QIODevice::Text when opening, you are not interpreting the content in any way.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    gunturrohith (27th November 2014)

Similar Threads

  1. Performance problem with QFile.
    By Abdeljalil in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2014, 18:25
  2. Replies: 2
    Last Post: 6th May 2013, 08:06
  3. QFile reading problem
    By giblit in forum Qt Programming
    Replies: 6
    Last Post: 5th April 2013, 05:45
  4. Problem with QFileDialog and QFile
    By Basti300 in forum Newbie
    Replies: 2
    Last Post: 26th May 2010, 20:18
  5. Problem with QFile
    By viciv919 in forum Newbie
    Replies: 5
    Last Post: 17th March 2010, 16:04

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
  •  
Qt is a trademark of The Qt Company.