Results 1 to 16 of 16

Thread: Unable to copy a file from one directory to another

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Unable to copy a file from one directory to another

    I am trying to copy a file from one directory to another

    Tried QFile::copy(). The file is copied but the size is zero Kb

    Tried placing a dummy file and then replacing with original

    But still the size is 0 Kb.

    Qt Code:
    1. QDir qd;
    2. QFile srcfile;
    3. QFile destifile;
    4.  
    5. QString dbsrcfilepath = qd.currentPath().append("/learnOn1.sqlite");
    6. dbsrcfilepath = QDir::toNativeSeparators(dbsrcfilepath);
    7. // dbsrcfilepath : "/Users/user/build-learnOn-Desktop_Qt_5_4_0_clang_64bit-Debug/learnOn.app/Contents/MacOS/learnOn1.sqlite"
    8. QString libpath = qd.homePath() + QDir::separator()+"learnOnContent" + QDir::separator();
    9. if(!(QDir(libpath).exists()))
    10. QDir().mkdir(libpath);
    11. QString dbdestinationfilepath = libpath.append("learnOn.sqlite");
    12. //dbdestinationfilepath : "/Users/user/learnOnContent/learnOn.sqlite"
    13.  
    14. dbdestinationfilepath = QDir::toNativeSeparators(dbdestinationfilepath);
    15.  
    16. srcfile.setFileName(dbdestinationfilepath);
    17. destifile.setFileName(dbsrcfilepath);
    18. if(!srcfile.exists())
    19. {
    20. bool success = true;
    21. success &= destifile.open( QFile::ReadOnly );
    22. success &= srcfile.open( QFile::WriteOnly | QFile::Truncate );
    23. qDebug()<<destifile.readAll().length();
    24. success &= srcfile.write( destifile.readAll() ) >= 0;
    25. destifile.close();
    26. srcfile.close();
    27. }
    28. else
    29. {
    30. srcfile.copy(dbdestinationfilepath);
    31. }
    To copy to clipboard, switch view to plain text mode 

    The copy is not proper. File size is always 0.

    Kindly help to resolve this issue.

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to copy a file from one directory to another

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Unable to copy a file from one directory to another

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QDir>
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QDir qd;
    9. QFile srcfile;
    10. QFile destifile;
    11.  
    12. QString dbsrcfilepath = "/Users/user/build-learnOn-Desktop_Qt_5_4_0_clang_64bit-Debug/learnOn.app/Contents/MacOS/learnOn1.sqlite";
    13. dbsrcfilepath = QDir::toNativeSeparators(dbsrcfilepath);
    14. destifile.setFileName(dbsrcfilepath);
    15. if(!(QDir("/Users/user/learnOnContent/").exists()))
    16. QDir().mkdir("/Users/user/learnOnContent/");
    17.  
    18. QString dbdestinationfilepath = "/Users/user/learnOnContent/learnOn.sqlite";
    19. dbdestinationfilepath = QDir::toNativeSeparators(dbdestinationfilepath);
    20. srcfile.setFileName(dbdestinationfilepath);
    21.  
    22. if(!srcfile.exists())
    23. {
    24. bool success = true;
    25. success &= destifile.open( QFile::ReadOnly );
    26. success &= srcfile.open( QFile::WriteOnly | QFile::Truncate );
    27.  
    28. success &= srcfile.write( destifile.readAll() ) >= 0;
    29. destifile.close();
    30. srcfile.close();
    31. }
    32. else
    33. {
    34. srcfile.copy(dbdestinationfilepath);
    35. }
    36.  
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

    copying from dbsrcfilepath to dbdestinationfilepath
    Last edited by ejoshva; 19th March 2015 at 12:56.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Unable to copy a file from one directory to another

    Line 34 tries to copy srcfile, which refers to the path dbdestinationfilepath, to dbdestinationfilepath. That seems unlikely to be productive.

  5. #5
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Unable to copy a file from one directory to another

    Thats fine, it has to be srcfile.copy(dbsrcfilepath);

    Firstly if true case is not working.
    Last edited by ejoshva; 19th March 2015 at 14:14.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to copy a file from one directory to another

    Quote Originally Posted by ejoshva View Post
    Thats fine
    Why is that fine? You cannot copy the file over itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Unable to copy a file from one directory to another

    What I meant was 'else' case is not the problem as it's written on the fly and I can correct it.

    I wanted to resolve the issue in the if success case first.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to copy a file from one directory to another

    Quote Originally Posted by ejoshva View Post
    What I meant was 'else' case is not the problem as it's written on the fly and I can correct it.

    I wanted to resolve the issue in the if success case first.
    I have no idea what you mean, sorry.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    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: Unable to copy a file from one directory to another

    Quote Originally Posted by ejoshva View Post
    I wanted to resolve the issue in the if success case first.
    You solved that yourself then, because the "then" case does not use QFile::copy().

    Cheers,
    _

  10. #10
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Unable to copy a file from one directory to another

    The else part was a mistake and I have corrected it as was pointed out.

    The priority is the then case.

    That's what I meant


    Added after 26 minutes:


    I removed the then clause and executed only with the else.
    Still it was not getting copied.
    File size 0Kb only.
    Last edited by ejoshva; 20th March 2015 at 05:33.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to copy a file from one directory to another

    I suggest you actually check the results of opening the source file before trying to read anything from it. Also if the file is large, there is a good chance your process will crash.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Unable to copy a file from one directory to another

    Got the issue resolved. Thanks for your inputs

    issue was with the write function.
    QFILE:const char *data, qint64 len)
    Upon using this function , the write is successful. Also data.size() is to be used and not data.length()

    The working code is below
    Qt Code:
    1. if(srcfile.exists())
    2. {
    3. bool success = true;
    4. success &= srcfile.open( QFile::ReadOnly );
    5. success &= destifile.open( QFile::WriteOnly | QFile::Truncate );
    6. success &= srcfile.write( destifile.readAll(),destifile.size()) >= 0;
    7. destifile.close();
    8. srcfile.close();
    9. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Unable to copy a file from one directory to another

    If you are copying large files, that's not a very good idea to destifile.readAll(). Why aren't you using QFile::copy() again? It seems trivial to:

    Qt Code:
    1. QFile destifile(dbdestinationfilepath);
    2. if (destifile.exists())
    3. {
    4. destifile.remove();
    5. }
    6. srcfile.copy(dbdestinationfilepath);
    To copy to clipboard, switch view to plain text mode 
    QFile::copy() won't overwrite a destination file, which is why I checked if it exists and remove it if it already exists. In the original code you posted, you are treating source and destination backwards IMHO... i.e. your open was truncating the source file which I doubt is what you actually intended.

    QFile::copy works, just make sure the destination file doesn't already exist and you can simplify your code.
    Last edited by jefftee; 25th March 2015 at 07:19.

  14. #14
    Join Date
    Jan 2019
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Unable to copy a file from one directory to another

    Hi ,

    Could you please share me the working code for copy a file from one directory to another.
    Thank you.






    Quote Originally Posted by ejoshva View Post
    Got the issue resolved. Thanks for your inputs

    issue was with the write function.
    QFILE:const char *data, qint64 len)
    Upon using this function , the write is successful. Also data.size() is to be used and not data.length()

    The working code is below
    Qt Code:
    1. if(srcfile.exists())
    2. {
    3. bool success = true;
    4. success &= srcfile.open( QFile::ReadOnly );
    5. success &= destifile.open( QFile::WriteOnly | QFile::Truncate );
    6. success &= srcfile.write( destifile.readAll(),destifile.size()) >= 0;
    7. destifile.close();
    8. srcfile.close();
    9. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Unable to copy a file from one directory to another

    Could you please share me the working code for copy a file from one directory to another.
    The code posted by jefftee does exactly that. Did you try it before making your post?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  16. #16
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to copy a file from one directory to another

    Apart from all the rest this was the initial problem
    Qt Code:
    1. qDebug()<<destifile.readAll().length();
    2. success &= srcfile.write( destifile.readAll() ) >= 0;
    To copy to clipboard, switch view to plain text mode 
    The file was read in completely during the qDebug() statement...

Similar Threads

  1. Replies: 2
    Last Post: 30th January 2015, 18:56
  2. Replies: 1
    Last Post: 27th December 2012, 07:01
  3. QtCreator unable to copy to the clipboard...
    By squimmy in forum Qt Tools
    Replies: 4
    Last Post: 25th December 2012, 13:53
  4. Replies: 2
    Last Post: 15th July 2010, 13:45
  5. Replies: 4
    Last Post: 24th February 2006, 11:30

Tags for this Thread

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.