3

I'm using the following code to write some text to a file:

QFile caFile(outputFolder + "file.extension");
caFile.open(QIODevice::WriteOnly | QIODevice::Text);

if(!caFile.isOpen()){
    qDebug() << "- Error, unable to open" << "outputFilename" << "for output";
}
QTextStream outStream(&caFile);
outStream << "First Line\nSecond Line\nThird Line";
caFile.close();

It's working like a charm, but with a little problem .. The text file should look like this:

First Line
Second Line
Third Line

But instead, it looks like this:

First Line

Second Line

Third Line

What's the problem here?

Alaa Salah
  • 1,047
  • 3
  • 12
  • 28

2 Answers2

3

It works for me like a charm.

input.txt

DA3MTkyMjE0NDdaFw0xODA2MDYyMjE0NDdaMDcxEzARBgNVBAMMCnVqY2E2bjku
anAxEzARBgNVBAoMCnZtamMgMm5vYjMxCzAJBgNVBAYTAlVTMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz0+a0BEJEkPwNq7BEplV81+++wzonVAWWcqe

main.cpp

#include <QTextStream>
#include <QFile>
#include <QDebug>
#include <QByteArray>

int main()
{
    QFile inFile("input.txt");
    inFile.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray inputData = inFile.readAll();

    QFile caFile("output.txt");
    caFile.open(QIODevice::WriteOnly | QIODevice::Text);

    if(!caFile.isOpen()){
        qDebug() << "- Error, unable to open" << "outputFilename" << "for output";
    }
    QTextStream outStream(&caFile);
    outStream << inputData;
    caFile.close();
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

output.txt

DA3MTkyMjE0NDdaFw0xODA2MDYyMjE0NDdaMDcxEzARBgNVBAMMCnVqY2E2bjku
anAxEzARBgNVBAoMCnZtamMgMm5vYjMxCzAJBgNVBAYTAlVTMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz0+a0BEJEkPwNq7BEplV81+++wzonVAWWcqe
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Actually, the same code works with some files perfectly without altering their look, but changes some of them as stated in the question. Weird! – Alaa Salah Jul 22 '14 at 22:40
  • 1
    It could be a matter of encoding or the editor you employ for inspecting the files. See `QIODevice::Text` for reference: `When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.` – Franz B. Mar 16 '15 at 13:55
  • For people coming back to this years later, troubleshooting this depends quite a lot on your text viewer. Notepad might display two lines while Notepad++ will display one line. Use Notepad++ with character visibility on to see what's happening. Usually this happens because the files are getting written with `\r\r\n` instead of `\r\n` on windows because newline characters aren't handled properly (often because you're manually doing it instead of letting Qt do it for you). – Phlucious Apr 23 '20 at 18:58
  • Where will the file be stored on mac book ? I'm not able to find it. Can you please help ? When i give the absolute path to my project dir, it is storing in my project dir. If i give just file name without any path, then i don't know where the file is getting stored. – K Pradeep Kumar Reddy Apr 20 '23 at 09:24
2
QString filename = "";//file adding path
QFile file(filename);
file.open(QIODevice::ReadWrite);
QTextStream stream(&file);
stream << "abc\nxyz" << endl;
file.close();


output:abc
       xyz
lucifer
  • 175
  • 14