在 Qt 中创建/写入新文件
我正在尝试写入文件,如果该文件不存在,则创建它。我在互联网上搜索过,但没有任何效果。
我的代码当前如下所示:
QString filename="Data.txt";
QFile file( filename );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
stream << "something" << endl;
}
如果我在目录中创建一个名为 Data 的文本文件,它仍然为空。如果我不创建任何内容,它也不会创建该文件。 我不知道该怎么办,这不是我尝试创建/写入文件的第一种方法,但没有一种方法有效。
I am trying to write into a file and if the file doesn't exist create it. I have searched on the internet and nothing worked for me.
My code looks currently like this:
QString filename="Data.txt";
QFile file( filename );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
stream << "something" << endl;
}
If I create a text file called Data in the directory, it remains empty. If I don't create anything it doesn't create the file either.
I don't know what to do with this, this isn't the first way in which I tried to create/write into a file and none of the ways worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您确定您位于正确的目录中吗?
打开没有完整路径的文件将在当前工作目录中打开它。在大多数情况下,这不是您想要的。尝试将第一行更改为
QString filename="c:\\Data.txt"
或QString filename="c:/Data.txt"
并查看该文件是否在
c:\
中创建Are you sure you're in the right directory?
Opening a file without a full path will open it in the current working directory. In most cases this is not what you want. Try changing the first line to
QString filename="c:\\Data.txt"
orQString filename="c:/Data.txt"
and see if the file is created in
c:\
这很奇怪,一切看起来都很好,你确定它不适合你吗?因为这个
main
肯定适合我,所以我会在其他地方寻找问题的根源。您提供的代码也与QTextStream详细描述中提供的代码几乎相同< /a> 所以我很确定问题出在其他地方:)
另请注意,该文件不称为
Data
而是Data.txt
并且应该创建/定位在运行程序的目录中(不一定是可执行文件所在的目录)。That is weird, everything looks fine, are you sure it does not work for you? Because this
main
surely works for me, so I would look somewhere else for the source of your problem.The code you provided is also almost the same as the one provided in detailed description of QTextStream so I am pretty sure, that the problem is elsewhere :)
Also note, that the file is not called
Data
butData.txt
and should be created/located in the directory from which the program was run (not necessarily the one where the executable is located).您的代码非常好,您只是没有在正确的位置查找文件。由于您没有提供绝对路径,因此将相对于当前工作文件夹(更准确地说是在您的情况下在当前工作文件夹中)创建您的文件。
您当前的工作文件夹由 Qt Creator 设置。转到项目>>您选择的版本>>按“运行”按钮(“构建”旁边),您将看到此页面上的内容,当然您也可以更改。
Your code is perfectly fine, you are just not looking at the right location to find your file. Since you haven't provided absolute path, your file will be created relative to the current working folder (more precisely in the current working folder in your case).
Your current working folder is set by Qt Creator. Go to Projects >> Your selected build >> Press the 'Run' button (next to 'Build) and you will see what it is on this page which of course you can change as well.
原因可能不是您没有找到正确的目录。例如,您可以从文件中读取(即使没有绝对路径),但似乎无法写入其中。
在这种情况下,您的程序可能在写入完成之前退出。
如果您的程序使用事件循环(如 GUI 应用程序,例如 QMainWindow),那么这不是问题。但是,如果程序在写入文件后立即退出,则应该刷新文本流,关闭文件并不总是足够的(而且这是不必要的,因为它在析构函数中关闭)。
这保证了在程序从该指令继续执行之前将更改提交到文件。
问题似乎是 QFile 在 QTextStream 之前被破坏。因此,即使在 QTextStream 析构函数中刷新流,也为时已晚,因为文件已经关闭。
It can happen that the cause is not that you don't find the right directory. For example, you can read from the file (even without absolute path) but it seems you cannot write into it.
In that case, it might be that you program exits before the writing can be finished.
If your program uses an event loop (like with a GUI application, e.g.
QMainWindow
) it's not a problem. However, if your program exits immediately after writing to the file, you should flush the text stream, closing the file is not always enough (and it's unnecessary, as it is closed in the destructor).This guarantees that the changes are committed to the file before the program continues from this instruction.
The problem seems to be that the QFile is destructed before the QTextStream. So, even if the stream is flushed in the QTextStream destructor, it's too late, as the file is already closed.