ctxrr
ctxrr

Reputation: 393

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?

After that , here is my problem, I don't know which API in Qt works just like "open" in 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.

I read some documents and found some classes such as QFile QDataStream but I don't know if they are exactly what I want.

Upvotes: 2

Views: 7162

Answers (2)

CMLDMR
CMLDMR

Reputation: 344

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;

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

arr is "Hello World QByteArray";

integer is 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.

Upvotes: 0

alediaferia
alediaferia

Reputation: 2617

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:

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.

You'd use QDataStream for reading binary data most of the times, like follows:

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 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. There's also plenty of examples.

Upvotes: 1

Related Questions