English 中文(简体)
在Qt中创建/写入新文件
原标题:Creating/writing into a new file in Qt
  • 时间:2011-02-06 21:17:53
  •  标签:
  • c++
  • qt

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

我的代码当前如下所示:

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.

谢谢你的回答。

最佳回答

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"

并查看该文件是否是在c:中创建的

问题回答

这很奇怪,一切看起来都很好,你确定这对你不起作用吗?因为这个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的详细描述所以我很确定,问题出在其他地方:)

还要注意的是,该文件不称为Data,而称为Data.txt并且应该创建/位于程序运行的目录中(不一定是可执行文件所在的目录)。

#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
";

    // 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;
}

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

您当前的工作文件夹由Qt Creator设置。转到项目>>;您选择的内部版本>>;按下“运行”按钮(构建旁边),您将看到该页面上的内容,当然您也可以更改。

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

在这种情况下,可能是在编写完成之前程序退出。

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

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

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

问题似乎是QFile在QTextStream之前被破坏了。因此,即使流在QTextStream析构函数中被刷新,也为时已晚,因为文件已经关闭。

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;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签