在 Qt 中创建/写入新文件

发布于 2024-10-16 06:53:56 字数 373 浏览 10 评论 0原文

我正在尝试写入文件,如果该文件不存在,则创建它。我在互联网上搜索过,但没有任何效果。

我的代码当前如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

江城子 2024-10-23 06:53:56

您确定您位于正确的目录中吗?
打开没有完整路径的文件将在当前工作目录中打开它。在大多数情况下,这不是您想要的。尝试将第一行更改为

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" or
QString filename="c:/Data.txt"

and see if the file is created in c:\

天涯沦落人 2024-10-23 06:53:56

这很奇怪,一切看起来都很好,你确定它不适合你吗?因为这个 main 肯定适合我,所以我会在其他地方寻找问题的根源。

#include <QFile>
#include <QTextStream>


int main()
{
    QString filename = "Data.txt";
    QFile file(filename);
    if (file.open(QIODevice::ReadWrite)) {
        QTextStream stream(&file);
        stream << "something" << endl;
    }
}

您提供的代码也与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.

#include <QFile>
#include <QTextStream>


int main()
{
    QString filename = "Data.txt";
    QFile file(filename);
    if (file.open(QIODevice::ReadWrite)) {
        QTextStream stream(&file);
        stream << "something" << endl;
    }
}

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 but Data.txt and should be created/located in the directory from which the program was run (not necessarily the one where the executable is located).

酒中人 2024-10-23 06:53:56
#include <QFile>
#include <QCoreApplication>
#include <QTextStream>

int main(int argc, char *argv[])
{
    // Create a new file     
    QFile file("out.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);
    out << "This file is generated by Qt\n";

    // optional, as QFile destructor will already do it:
    file.close(); 

    //this would normally start the event loop, but is not needed for this
    //minimal example:
    //return app.exec();

    return 0;
}
#include <QFile>
#include <QCoreApplication>
#include <QTextStream>

int main(int argc, char *argv[])
{
    // Create a new file     
    QFile file("out.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);
    out << "This file is generated by Qt\n";

    // optional, as QFile destructor will already do it:
    file.close(); 

    //this would normally start the event loop, but is not needed for this
    //minimal example:
    //return app.exec();

    return 0;
}
无声静候 2024-10-23 06:53:56

您的代码非常好,您只是没有在正确的位置查找文件。由于您没有提供绝对路径,因此将相对于当前工作文件夹(更准确地说是在您的情况下在当前工作文件夹中)创建您的文件。

您当前的工作文件夹由 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.

enter image description here

心不设防 2024-10-23 06:53:56

原因可能不是您没有找到正确的目录。例如,您可以从文件中读取(即使没有绝对路径),但似乎无法写入其中。

在这种情况下,您的程序可能在写入完成之前退出。

如果您的程序使用事件循环(如 GUI 应用程序,例如 QMainWindow),那么这不是问题。但是,如果程序在写入文件后立即退出,则应该刷新文本流,关闭文件并不总是足够的(而且这是不必要的,因为它在析构函数中关闭)。

stream << "something" << endl;
stream.flush();

这保证了在程序从该指令继续执行之前将更改提交到文件。

问题似乎是 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).

stream << "something" << endl;
stream.flush();

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.

驱逐舰岛风号 2024-10-23 06:53:56
QFile file("test.txt");
/*
 *If file does not exist, it will be created
 */
if (!file.open(QIODevice::ReadOnly | QIODevice::Text | QIODevice::ReadWrite))
{
    qDebug() << "FAILED TO CREATE FILE / FILE DOES NOT EXIST";
}

/*for Reading line by line from text file*/
while (!file.atEnd()) {
    QByteArray line = file.readLine();
    qDebug() << "read output - " << line;
}

/*for writing line by line to text file */
if (file.open(QIODevice::ReadWrite))
{
    QTextStream stream(&file);
    stream << "1_XYZ"<<endl;
    stream << "2_XYZ"<<endl;
}
QFile file("test.txt");
/*
 *If file does not exist, it will be created
 */
if (!file.open(QIODevice::ReadOnly | QIODevice::Text | QIODevice::ReadWrite))
{
    qDebug() << "FAILED TO CREATE FILE / FILE DOES NOT EXIST";
}

/*for Reading line by line from text file*/
while (!file.atEnd()) {
    QByteArray line = file.readLine();
    qDebug() << "read output - " << line;
}

/*for writing line by line to text file */
if (file.open(QIODevice::ReadWrite))
{
    QTextStream stream(&file);
    stream << "1_XYZ"<<endl;
    stream << "2_XYZ"<<endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文