Results 1 to 4 of 4

Thread: Program doesn't find .txt file when started directly.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Program doesn't find .txt file when started directly.

    Hello!

    I'm trying to read a .txt file in my program, so, to do that i created a "Database/test.txt" file in "build-Loterias-Desktop-Debug" folder. When i run my program using QTCreator, he finds my .txt file, but when i run it directly without using QtCreator he finds nothing!

    How can i solve the problem?

    Here my code.
    Qt Code:
    1. #define LF_PATH "Database/test.txt"
    2. obj.createTestData( LF_PATH );
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Test::createTestData(QString filePath) {
    2. if( !filePath.isEmpty( ) ) {
    3. QFile file( filePath );
    4.  
    5. if( file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
    6. QTextStream stream( &file );
    7.  
    8. // Rest of the code...
    9.  
    10. file.close( );
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Program doesn't find .txt file when started directly.

    Check out QDir::currentPath();

    You are working with relative paths.
    An absolute path would solve the problem however, you could solve your problem using relative paths.

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

    robgeek (18th August 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Program doesn't find .txt file when started directly.

    Is that really necessary? Because in C or even in C++ all you need to do is put the .txt file in the same bin's folder.

    That's why i created a "Database/test.txt" in "build-Test-Desktop-Debug" folder.

  5. #4
    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: Program doesn't find .txt file when started directly.

    A Qt application is a C++ application.
    All applications, independent of which language they are written in, have a "current working directory" (CWD), on which they base relative paths.

    Which directory that is depends on how it is launched.

    When launched from a shell, the CWD usually is the CWD of the shell at that moment.
    When launched from an application launcher ("Start menu") it is usually the user's home folder.

    If you want to use the path of where the application is located, see QCoreApplication::applicationDirPath()

    Cheers,
    _

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

    robgeek (18th August 2015)

Similar Threads

  1. Replies: 11
    Last Post: 18th September 2012, 11:15
  2. Replies: 5
    Last Post: 15th June 2010, 08:42
  3. Program result writed by qDebug() to file doesn't work.
    By Vincenzo in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2009, 20:49
  4. Statically linked program doesn't load QImage from file.
    By badjer1024 in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2009, 19:45
  5. Replies: 5
    Last Post: 19th September 2008, 16:24

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.