Results 1 to 15 of 15

Thread: Save File in another Folder

  1. #1
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Save File in another Folder

    Hi Guys,

    I have a file which I want to save.
    With: fstream File (fileName, ios:ut | ios::trunc | ios::binary);
    I can save the file in the directory in which my Programm runs.
    But I want to save it in a subfolder called "log"
    This subfolder should be in the same directory in which my program runs,

    I thought its not that hard, I tried to change the name of my file to "/log/filename"
    But I simply cant, I dont know how to change my char* to this.

    I hope you understood where my problem lies and can help me. thank you

  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: Save File in another Folder

    Use std::string for string manipulation.

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

    Default Re: Save File in another Folder

    God I knew I had to do this...
    Ok, Thanks

  4. #4
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save File in another Folder

    Ok, I found out that you cant create directories with iostream.
    I found the class QDir, but I dont know how to create a directory...
    I tried it with:

    Qt Code:
    1. QDir* Dir = new QDir("logs");
    2. if (!Dir->exists())
    3. {
    4. Dir->mkdir("../logs/");
    5. }
    To copy to clipboard, switch view to plain text mode 

    And with Dir->mkdir("logs"); , ... and so on. But a directory was never created.
    How to do this???
    The directory should be created in the same directory where my applications runs.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save File in another Folder

    Quote Originally Posted by GonzoFist View Post
    The directory should be created in the same directory where my applications runs.
    Then why are you using '..' ? Just use a relative path, or better, get the application path on startup and create an absolute dir specification.

  6. #6
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save File in another Folder

    How can I get the relative path or the absolute directory?
    Usually I understand the QT Documentation, but with QDir I got some problems in understanding.

  7. #7
    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: Save File in another Folder

    Absolute path is one starting with the root of the filesystem ("/"), relative is everything else. So if you are currently in "/a/b/c" and ask for the directory "d" then "d" is a relative path and "/a/b/c/d" is an absolute path both pointing to the same resource.

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

    Default Re: Save File in another Folder

    I have already read the differnce between absolut and relative. (And I think I understood the difference, its not that complicated)
    But I dont know how to work with them.
    For Example, why doesnt work my Code below?

    Qt Code:
    1. QDir* Dir = new QDir("logs");
    2. QString test = Dir->absoluteFilePath("t");
    3. if (!Dir->exists())
    4. {
    5. Dir->mkdir(test);
    6. }
    To copy to clipboard, switch view to plain text mode 

    With the command absoluteFilePath("t") I get the absolute path where my directory should be.
    But my Directory still isnt created.

  9. #9
    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: Save File in another Folder

    Your code is invalid. Call QDir::mkdir() with "t" and not the value of test. And don't create QDir objects on the heap, it's not java.

  10. #10
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save File in another Folder

    Qt Code:
    1. QDir Dir("logs");
    2. if (!Dir.exists())
    3. {
    4. Dir.mkdir("t");
    5. }
    To copy to clipboard, switch view to plain text mode 

    Still doesnt work?! I begin hardly feeling stupid...

  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: Save File in another Folder

    What does Dir.absolutePath() return?

    By the way, your code still doesn't make much sense - it says "if in the current directory there is no 'logs' subdirectory, create a 't' subdirectory". Is that really what you want?

  12. #12
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save File in another Folder

    Yeah I know the code doesnt make much sense, but I tried at least to create a Directory. In my finished programm it will create a sub folder called "logs".

    With Dir.absolutePath() I get: /home/marco/workspace/IPA/build/logs/t

    But in Build there is no folder called "logs" or "t".
    Maybe I need root rights or something like that?
    I could also post my whole programm but its large :S

  13. #13
    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: Save File in another Folder

    I'm not sure how would you want to create a subdirectory of a directory that doesn't exist...

    Qt Code:
    1. #include <QDir>
    2. #include <QtDebug>
    3.  
    4. int main(){
    5. QDir dir("logs");
    6. qDebug() << "Dir:" << dir.absolutePath();
    7. qDebug() << "Exists:" << dir.exists();
    8. if(!dir.exists()){
    9. // if dir doesn't exist, you can't create a subdirectory in it
    10. // [1]
    11. dir = QDir::current();
    12. qDebug() << "Current dir:" << dir.absolutePath();
    13. if(dir.exists()){
    14. qDebug() << "Current dir exits...";
    15. dir.mkdir("logs");
    16. }
    17. // [2]
    18. // dir.mkpath("logs");
    19. }
    20. QDir dircheck("logs");
    21. qDebug() << "Exists:" << dircheck.exists();
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 

    Use either [1] or [2].

  14. The following user says thank you to wysota for this useful post:

    GonzoFist (20th April 2010)

  15. #14
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save File in another Folder

    Ah yes of course, I cant create a subdirectory in a not existing folder!
    I changed my Code to
    Qt Code:
    1. QDir dir("logs");
    2. if(!dir.exists())
    3. {
    4. dir = QDir::current();
    5. dir.mkdir("logs");
    6. }
    To copy to clipboard, switch view to plain text mode 

    And now it works just as I wanted it to.
    Thanks wysota, your the man!

  16. #15
    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: Save File in another Folder

    This would be simpler:
    Qt Code:
    1. QDir::current().mkpath("logs");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Moving File/Folder in Qt
    By arpspatel in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2010, 02:37
  2. Release folder - 1 .exe file only
    By Swankee in forum Qt Tools
    Replies: 6
    Last Post: 21st December 2009, 23:43
  3. Can't create folder if file with same name exists
    By Barry79 in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2009, 17:33
  4. Replies: 2
    Last Post: 27th October 2007, 19:16
  5. Password on local file/folder
    By icebox25 in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2007, 17:33

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.