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] Write to a file without the CR at the end of line
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Write to a file without the CR at the end of line

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.9k 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.
  • saeedhardanS Offline
    saeedhardanS Offline
    saeedhardan
    wrote on last edited by
    #1

    hi,
    i have a file that contains "text" , and every line in it ends with ONLY LF.
    i'm reading some parts of it and writing to a file using :
    @
    QTextStream tmp_in(old_file);
    QTextStream stream(new_file);
    QString tmp_line = tmp_in.readLine();
    stream << tmp_line << endl;
    @
    but the new written data contains CR,LF at end of each line .
    how can i write without the CR ??

    i'm using windows .

    By LF and CR i mean carriage return and line feed

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nologinma
      wrote on last edited by
      #2

      try in this way:

      @QTextStream tmp_in(old_file);
      QTextStream stream(new_file);
      QString tmp_line = tmp_in.readLine();
      tmp_line.simplified();
      stream << tmp_line << endl;@

      1 Reply Last reply
      0
      • saeedhardanS Offline
        saeedhardanS Offline
        saeedhardan
        wrote on last edited by
        #3

        [quote author="nologinma" date="1409666663"]try in this way:

        @QTextStream tmp_in(old_file);
        QTextStream stream(new_file);
        QString tmp_line = tmp_in.readLine();
        tmp_line.simplified();
        stream << tmp_line << endl;@[/quote]

        It didn't work :\

        i tried QDataStream , i does the work writing only the LF but also adds NUL between every character of the file .

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi,
          On Win machines you get the CR/LF all the time and it is good practice on Linux machines to do the same. Then text files are able to be transported from Win to Linux and back.
          Is there a reason you don't want the CR?

          Greetz, Jeroen

          1 Reply Last reply
          0
          • saeedhardanS Offline
            saeedhardanS Offline
            saeedhardan
            wrote on last edited by
            #5

            solved part of the problem it i just removed the
            [quote]
            | QIODevice::Text
            [/quote]
            from the open , now my open for writing is :
            @
            new_file->open(QIODevice::WriteOnly);@

            so my writing now is like this :
            @
            QFile* new_file= new QFile("XXXX");
            new_file->open(QIODevice::WriteOnly);

            QTextStream tmp_in(old_file);
            QTextStream tmp_out(new_file);
            
            QString tmp_line = tmp_in.readLine();
            while(tmp_line != ")"){
                tmp_out << tmp_line + "\n";
                tmp_line = tmp_in.readLine();
            }
            tmp_out << ")" << endl;
            

            @

            the only problem now is that the endl , writes "\n" in last line which i dont want , can i modify the endl so it doesn't do so ?

            [quote]
            Is there a reason you don’t want the CR?
            [/quote]
            i have to , i need to output my file according to a format .

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nologinma
              wrote on last edited by
              #6

              Ok, then please put [SOLVED] as prefix to Post name.

              1 Reply Last reply
              0
              • saeedhardanS Offline
                saeedhardanS Offline
                saeedhardan
                wrote on last edited by
                #7

                [quote author="nologinma" date="1409730610"]Ok, then please put [SOLVED] as prefix to Post name.[/quote]

                sorry i thought i solved but then , i got the last line problem see my last post above .

                1 Reply Last reply
                0
                • saeedhardanS Offline
                  saeedhardanS Offline
                  saeedhardan
                  wrote on last edited by
                  #8

                  Now solved instead of using endl;
                  i just used flush .
                  thank you all .

                  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