PDA

View Full Version : Program doesn't find .txt file when started directly.



robgeek
18th August 2015, 03:26
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.

#define LF_PATH "Database/test.txt"
obj.createTestData( LF_PATH );

void Test::createTestData(QString filePath) {
if( !filePath.isEmpty( ) ) {
QFile file( filePath );

if( file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
QTextStream stream( &file );

// Rest of the code...

file.close( );
}
}
}

Thanks!

Qiieha
18th August 2015, 08:19
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.

robgeek
18th August 2015, 13:00
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.

anda_skoa
18th August 2015, 13:28
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,
_