I cannot read from text file
-
I need to read text.txt file that located on my application directory. Here 's my code about read file. When I try debug version. I only see" " .
when I debug appDir I see directory what I want. I did not understand where is my mistake.. Can someone help?QString appDir = QDir::currentPath() + "/test.txt"; QString version; QFile file(appDir); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in ( & file); version = in .readLine(); file.close(); } qDebug() << version; qDebug() << appDir; }
-
I need to read text.txt file that located on my application directory. Here 's my code about read file. When I try debug version. I only see" " .
when I debug appDir I see directory what I want. I did not understand where is my mistake.. Can someone help?QString appDir = QDir::currentPath() + "/test.txt"; QString version; QFile file(appDir); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in ( & file); version = in .readLine(); file.close(); } qDebug() << version; qDebug() << appDir; }
@mangekoyu said in I cannot read from text file:
I need to read text.txt file that located on my application directory
is it really? or is it at your source tree/project file directory. Those are 2 are different things.
QDir::currentPath() uses the path of the executable that was compiled and is usually in a shadow build directory parallel to your source tree directory.And it may be even more complicated when you're on a Mac, I assume this is Windows?
print
QDir::currentPath()
via QDebug and check if its the path you expect -
@mangekoyu said in I cannot read from text file:
I need to read text.txt file that located on my application directory
is it really? or is it at your source tree/project file directory. Those are 2 are different things.
QDir::currentPath() uses the path of the executable that was compiled and is usually in a shadow build directory parallel to your source tree directory.And it may be even more complicated when you're on a Mac, I assume this is Windows?
print
QDir::currentPath()
via QDebug and check if its the path you expect@J-Hilk I checked with qDebug()
and saw exactly the path I wanted.I download a txt file from the internet and it automatically saves it to the src folder.
QDir::currentPath() gives me the path of the src/ folder. My problem here is that the file I'm trying to read is in the src folder?
-
So, if you are 100% that the file is there, and that it does indeed have name
test.txt
, then are you also sure that the first line of that file contains any text? You are only reading the first line so if it's empty,version
will be an empty string for sure, too.To make sure your file gets opened, change the code to (for example):
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in ( & file); version = in .readLine(); file.close(); } else { qDebug() << "ERROR! Could not open file at:" << appDir; }
-
I need to read text.txt file that located on my application directory. Here 's my code about read file. When I try debug version. I only see" " .
when I debug appDir I see directory what I want. I did not understand where is my mistake.. Can someone help?QString appDir = QDir::currentPath() + "/test.txt"; QString version; QFile file(appDir); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in ( & file); version = in .readLine(); file.close(); } qDebug() << version; qDebug() << appDir; }
@mangekoyu
Your code will print empty string forversion
without any warning or indication if it cannot open the file. Don't you think your code should report that situation like @sierdzio says?QDir::currentPath() gives me the path of the src/ folder.
If it does that's "by coincidence". Might work from Qt Creator. Won't work if you run from a terminal. It's the application's current directory. Which you don't know (depends how it's run) and could be anything at any time. You cannot reliably locate a source folder you use in development at runtime from an application. And when deployed it may not even be present. In short, don't use it.
Finally:
version = in .readLine();
What are you expecting this to return anyway. It's the first line of a file. How do we know whether the file has a blank/no first line?