无法使用 ofstream 写入 Windows 7 中的临时文件夹

Can't write to temp folder in Windows 7 using ofstream

本文关键字:文件夹 Windows ofstream 写入      更新时间:2023-10-16

当我使用QT时,我使用QT-Way获取Windows Temp文件夹。这有效,但是看起来我无法写入此文件夹。

这是代码:

   const QStringList defaultFolders = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
   const QString tempFolder = defaultFolders.first();
   const QString fileName = tempFolder + "/testFile.txt";
   ofstream file(fileName.toLocal8Bit(), fstream::out);
   const bool ok = file.is_open();
   const QString msg = ok ? "Success" : "Failure";
   QMessageBox::warning(this, fileName, msg);

我到目前为止尝试了

  • 使用" \"的硬编码路径而不是"/"
  • 使用.tostDstring()。c_str()而不是" .tolocal8bit()

注释

  • 路径中有一个突出的字符。
  • Windows用户是管理员
  • 我可以在临时文件夹中手动创建/删除文件,但是编译的应用程序不能。
  • 我正在虚拟机中运行Windows 7
  • 如果我使用旧的C函数(Fopen/fwrite/fclose)
  • ,它的工作正常

有人知道出了什么问题吗?

也许使用toStdWString()代替toLocal8Bit()会更好吗?

std::filesystem::path filePath(fileName.toStdWString());
std::ofstream file(filePath);