Qt资源文件中的txt无法使用fstream读取
qrc中的txt文件是只读的,在使用fstream
读取冒号开头的资源文件时,会出现读取失败,即使设置了ios::in
。
fstream file;
file.open(":/xxx.txt", ios::in);
if (file.is_open())
{
string s;
while (getline(file, s))
{
...
}
file.close();
}
因此建议使用QFile
来读取,可以正常读取。
QFile file(":/xxx.txt");
file.open(QIODevice::ReadOnly);
while (!file.atEnd())
{
QByteArray data = file.readLine(); // 读取一行
std::string str = QString(data).toStdString(); // 转为标准string
...
}
file.close();