QFile不打开/查找文件 [英] QFile not opening/finding file

查看:547
本文介绍了QFile不打开/查找文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小文件打开函数,使用 QFile ,但在调试后,我注意到它跳过的功能 - 与没有找到文件或编码错误?

I have this small file opening function, using QFile, but after debugging i've noticed that its skipping the function - something to do with not finding the file or a coding error?

QString s;
QFile file(QString::fromStdString(fileName));
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
    QTextStream in(&file);
    while (!in.atEnd())
    {
        s = in.readLine();
        shipHandler::lineParse(s);
    }
}

试图打开的文件是一个文本文件,它位于主文件夹中所有其他标题+源文件。我将file.txt传递给此函数。

The file im trying to open is a text file, and its located in the main folder with all the other headers + source files. I'm passing "file.txt" to this function.

推荐答案

我怀疑问题是与 QFile 没有找到文件。如果未指定完整路径,QFile将查找当前目录。当前目录是: QDir :: currentPath()

I suspect the problem is with QFile not finding the file. If the full path is not specified, QFile will look in the current directory. The current directory is: QDir::currentPath()

所以首先尝试如果它工作,如果你提供完整路径,如果它的工作,你将需要把'ships.txt'在正确的路径。

So first try if it works if you supply the complete path, if it works you will need to put the 'ships.txt' in the correct path.

我通常在项目中做的是将文件放在同一个文件夹的EXE(或在与EXE相同的目录中的文件夹)。

What I normally do in projects is place the file in the same folder of the EXE (or in a folder in same directory as the EXE). Then retrieve the path of the EXE and use it the form an new path to the file.

例如:(未测试!) / p>

For example: (Not tested!)

QString exePath = QCoreApplication::applicationDirPath()
QString shipsPath = exePath + QDir::separator() + 'ships.txt'

这篇关于QFile不打开/查找文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆