简体   繁体   English

Qt5:如何在本地文件系统中读取/写入文件

[英]Qt5: How to read/write the file in local file system

I'm new to Qt.In my app,I want to press a button and it will come out a QFileDialog to let me select the file in file system .So how to do that? 我是Qt的新手。在我的应用程序中,我想按一个按钮,它将出现QFileDialog,让我选择文件系统中的文件。那么该怎么做?

After that , here is my problem, I don't know which API in Qt works just like "open" in POSIX ? 之后,这就是我的问题,我不知道Qt中的哪个API就像POSIX中的“打开”一样工作? I think if I can open the file in the right way , this API will return me a file descriptor and I can read/write this file like open did in posix. 我认为,如果我可以以正确的方式打开文件,则此API将返回一个文件描述符,并且可以像posix中的open一样读写该文件。

I read some documents and found some classes such as QFile QDataStream but I don't know if they are exactly what I want. 我阅读了一些文档,发现了一些类,例如QFile QDataStream,但我不知道它们是否正是我想要的。

Those are exactly what you are looking for. 这些正是您要寻找的。 In particular, you can use some of the static methods of QFileDialog to get a reference to the file you want to open, like: 特别是,您可以使用QFileDialog的某些静态方法来获取对要打开的文件的引用,例如:

static QString  getOpenFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)

and then use a QFile and QDataStream or QTextStream to read the contents. 然后使用QFileQDataStreamQTextStream读取内容。

You'd use QDataStream for reading binary data most of the times, like follows: 您通常会使用QDataStream读取二进制数据,如下所示:

QFile f(fileName);
if (f.open(QIODevice::ReadOnly)) {
    QDataStream stream(&f);
    int data;
    stream >> data;
}

Otherwise you can read plain text with QTextStream as follows: 否则,您可以使用QTextStream读取纯文本,如下所示:

QTextStream stream(&f);
QString line;
do {
    line = stream.readLine();
    /* do something with the line */
} while (!line.isNull());

Qt docs are pretty complete, you just have to take your time and read them. Qt文档非常完整,您只需要花点时间阅读它们。 There's also plenty of examples . 还有很多例子

Only Reading: 仅阅读:

QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open file"), "", tr("all Files ()"));

QFile file(fileName);

if(file.open(QIODevice::ReadOnly)){
QByteArray arr = file.readAll();
file.close();
}

Only Writing: 只写:

QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open file"), "", tr("all Files ()"));

QFile file(fileName);

if(file.open(QIODevice::WriteOnly)){
file.write(QBtyeArray("Heelo World"));
file.close();
}

Read and Write: 读和写:

QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open file"), "", tr("all Files ()"));

QFile file(fileName);

if(file.open(QIODevice::ReadWrite)){
QByteArray arr = file.readAll();
arr += " From Earth";
file.write(arr);
file.close();
}

if you use QDatastream you do not need resolve how many part you have written before,follow Below Code and I always use this method; 如果您使用QDatastream,则不需要解析之前编写的部分,请遵循下面的代码,我始终使用此方法。

QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
QDatastream out(&buffer);

out << QString("Hello World QString");
out << QByteArray("Hello World QByteArray");
out << int(55);

buffer.close();

QFile file(fileName);
if(file.open(QIIDevice::WriteOnly)){

file.write(buffer.data());
file.close();
}

and reading this file 并读取此文件

QFile file(fileName);
if(file.open(QIIDevice::WriteOnly)){

QDatastream in(&file);

QString str;
QByteArray arr;
int integer;

in >> str;
in >> arr;
in >> integer;

file.close();

}

str is "Hello World QString"; str是“ Hello World QString”;

arr is "Hello World QByteArray"; arr是“ Hello World QByteArray”;

integer is 55; 整数是55;

QDataStream is adding extra bytes to file for your parts and if you read it with QDataStream, QDataStream solve how many parts and each part bytes instead of you. QDataStream为您的零件添加了额外的字节,如果您使用QDataStream读取它,QDataStream会代替您来解决多少零件和每个零件字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM