Newline character into textfile using QFile
Asked Answered
B

3

8

i want to put a newline into a txt file, i tried with many alternative with so many help from this forum but i am getting always unknown character always. Please help

for (int i = 0; i < fileDet.size(); i++) {
  qDebug() << "Name directory" << fileDet.at(i);
  QFile data("output.txt");
  if (data.open(QFile::Append)) {
    QTextStream out(&data);
    out << fileDet.at(i);  //<<'\n';
    out << QChar((int)'\n');
  }
  data.close();
}
Bethezel answered 9/4, 2010 at 6:20 Comment(1)
What do you mean by "i am getting always unknown character" ?Amontillado
A
13

Try out << endl;

Abecedarium answered 9/4, 2010 at 6:46 Comment(1)
It's making flush, performance suffers, write '\n' instead or use putCharAdalard
H
8

When you open the file:

data.open(QFile::Append | QFile::Text)

Houlberg answered 9/4, 2010 at 15:44 Comment(2)
That fixes the issue. But how?Extreme
Do NOT work if you are using pure QFile, F.E. QFile file(path); file.open(QIODevice::Append | QFile::Text); file.write("111111"); file.write("2222222");file.close(); out is on one! line.Adalard
A
0

Very fast way using QFile::putChar :

file.putChar('\n');
Adalard answered 18/7, 2023 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.