QString write to file

QString write to file, 7.0 out of 10 based on 9 ratings
VN:F [1.9.22_1171]
Rating: 7.0/10 (9 votes cast)

Yesterday I did a lot of research about how to use a QString as input of file.write().
All what I found was stupid recommendations to do it with QTextStream but I didn’t like it (code looks ugly :D).

So what I did is I took a look at the manual for the input value and then wrote it by my own.

Here is the solution how you use a QString as input of file.write():

QString input = “abcdefg”;
QFile file(“/home/write.test”);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write((const char *)input.toAscii().data());

The solution you can find is:

QFile file(“/home/write.test”);
file.open(QIODevice::WriteOnly | QIODevice::Text);

QTextStream out(&file);
out << “abcdefg”;

This entry was posted in QT. Bookmark the permalink.

5 Responses to QString write to file

  1. frostwire download says:

    trying to find you on facebook, what’s your profile

  2. fcixpu says:

    I bookmarked this link. Thank you for good job!

  3. HmG0HzN0 says:

    I want to post quick hello and want to say appriciate for this good article.

  4. Ian Monroe says:

    maybe if you don’t care about UTF-8, you should just use QByteArray in the first place.

  5. Roland says:

    Your solution for file.write() does not work if data is really Unicode, having foreign language values which don’t fit into the ASCII character set.

Leave a Reply

Your email address will not be published. Required fields are marked *