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. How to add data in .txt line by line

How to add data in .txt line by line

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.1k 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.
  • M Offline
    M Offline
    mark_ua_1999
    wrote on last edited by
    #1

    I would like to add data in .txt file line by line how could I have tried the following code

    QString filename="Data.txt";
    QFile file( filename );
    if ( file.open(QIODevice::Append) )
    {
    QTextStream stream( &file );
    stream << s<<'\n' << endl;
    }
    For example file data is "Kamianske_Kropyvnytskyi_KharkivKherson_Kryvyi0Rih_Kharkiv_khmelnytskyi"
    but I would like they to be
    "Kamianske_Kropyvnytskyi_Kharkiv"
    "Kherson_Kryvyi0Rih_Kharkiv_khmelnytskyi"
    to read them line by line afterward
    but it add data in the same line, hope your ideas will help me to resolve the problem, thanks.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      the << endl; is correct.
      You are most likely using notepad which only likes \r\n

      https://superuser.com/questions/362087/notepad-ignoring-linebreaks

        QString filename = "Data.txt";
        QFile file( filename );
        if ( file.open(QIODevice::Append) ) {
          QTextStream stream( &file );
          for (int cc = 0; cc < 10; ++cc) {
          stream << QString::number(cc) << "\r\n";
          }
        }
      
      M 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        the << endl; is correct.
        You are most likely using notepad which only likes \r\n

        https://superuser.com/questions/362087/notepad-ignoring-linebreaks

          QString filename = "Data.txt";
          QFile file( filename );
          if ( file.open(QIODevice::Append) ) {
            QTextStream stream( &file );
            for (int cc = 0; cc < 10; ++cc) {
            stream << QString::number(cc) << "\r\n";
            }
          }
        
        M Offline
        M Offline
        mark_ua_1999
        wrote on last edited by
        #3

        @mrjj the following code resolve the problem
        QString filename="Data.txt";
        QFile file( filename );
        if ( file.open(QIODevice::Append|QIODevice::Text) )
        {
        QTextStream stream( &file );
        stream << s<<'\n';
        }
        file.close();

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok. super
          Please mark as solved.

          My notepad must be extra stupid then :)
          alt text

          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