Results 1 to 16 of 16

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 11: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 13: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,
    _

Similar Threads

  1. Replies: 2
    Last Post: 30th January 2015, 17:56
  2. Replies: 1
    Last Post: 27th December 2012, 06:01
  3. QtCreator unable to copy to the clipboard...
    By squimmy in forum Qt Tools
    Replies: 4
    Last Post: 25th December 2012, 12:53
  4. Replies: 2
    Last Post: 15th July 2010, 12:45
  5. Replies: 4
    Last Post: 24th February 2006, 10: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.