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. Problem creating text file
Forum Updated to NodeBB v4.3 + New Features

Problem creating text file

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 236 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I am having a problem creating a text file, I populated a QStringList with the rows I want to print, I have verified that each row ends only with \r\n using Qt Creator.

    Here is the code that writes the list to the file:

        QFile fCSV(strFilename);
        if ( fCSV.open(QIODevice::ReadWrite | QIODevice::Text) == true )
        {
            QTextStream tsCSV(&fCSV);
            foreach( strRow, slstRows )
            {
                tsCSV << strRow;
            }
            fCSV.close();
        }
    

    What I'm seeing when I open this file in an editor that shows end of line terminators is that each line ends with \r\r\n, where is this coming from?

    I've looked at the code and I only ever append \r\n once to each string and as I said using Qt Creator the bytes ending on the QString end only in 13 (\r) followed by 10 (\n).

    Yet when it's written to file this is changed to \r\r\n.

    Kind Regards,
    Sy

    raven-worxR 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I am having a problem creating a text file, I populated a QStringList with the rows I want to print, I have verified that each row ends only with \r\n using Qt Creator.

      Here is the code that writes the list to the file:

          QFile fCSV(strFilename);
          if ( fCSV.open(QIODevice::ReadWrite | QIODevice::Text) == true )
          {
              QTextStream tsCSV(&fCSV);
              foreach( strRow, slstRows )
              {
                  tsCSV << strRow;
              }
              fCSV.close();
          }
      

      What I'm seeing when I open this file in an editor that shows end of line terminators is that each line ends with \r\r\n, where is this coming from?

      I've looked at the code and I only ever append \r\n once to each string and as I said using Qt Creator the bytes ending on the QString end only in 13 (\r) followed by 10 (\n).

      Yet when it's written to file this is changed to \r\r\n.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @SPlatten
      Qt does it. it appends the native platforms convention for line ending.
      Since you are on windows i assume, you get the "\n" replaced with "\r\n"

      Dont use QTextStream, but write the binary data directly:

          QFile fCSV(strFilename);
          if ( fCSV.open(QIODevice::WriteOnly)  )
          {
              for( const QString & strRow : qAsConst(slstRows) )
                  fCSV.write( strRow.toUtf8() );
              fCSV.close();
          }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      SPlattenS 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @SPlatten
        Qt does it. it appends the native platforms convention for line ending.
        Since you are on windows i assume, you get the "\n" replaced with "\r\n"

        Dont use QTextStream, but write the binary data directly:

            QFile fCSV(strFilename);
            if ( fCSV.open(QIODevice::WriteOnly)  )
            {
                for( const QString & strRow : qAsConst(slstRows) )
                    fCSV.write( strRow.toUtf8() );
                fCSV.close();
            }
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @raven-worx Thank you!

        Kind Regards,
        Sy

        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