Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Appending to the end of a text file
Forum Updated to NodeBB v4.3 + New Features

[Solved] Appending to the end of a text file

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 20.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    In the following code snippet, there's a QString called parameterString that contains a few lines of information, and I'm trying to append it to the end of a text file.

    I was using QFile::Append before, but it kept adding a phantom carriage return/line feed in between the previous end of file and the new data.
    @
    QString parameterFileString("../sdcard/ParameterLog_1.csv");
    QFile parameterFile(parameterFileString);
    if(parameterFile.open(QFile::WriteOnly | QFile::Text))
    {
    parameterFile.seek(parameterFile.size());
    QTextStream out(&parameterFile);
    out << parameterString;
    parameterFile.close();
    }
    @

    This gives me weird results. Some of the data from the original file is there but not all. What am I doing wrong?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Opening the file WriteOnly will discard its existing content. If you seek to position n and write you will get n zero bytes in the file, followed by whatever you write.

      You want to append so you should open the file for appending.

      As for the "phantom CRLF". If the existing data was written with QTextStream then you should not have magically added CR LF characters. Many text editors will add CR LF to an incomplete last line.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved