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 read only first words from each line of file and add them to another file?

How to read only first words from each line of file and add them to another file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 830 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.
  • A Offline
    A Offline
    Annatar
    wrote on last edited by
    #1

    Hello. I have a

    Qfile myfile("a.txt");
    

    which contents:
    a 123
    b 123
    c 123

    I want to add only a, b, c to my another file;
    Tryied to do it in this way:

        QFile myfile("a.txt");
        myfile.open(QIODevice::ReadOnly | QIODevice::Text);
    
        QTextStream out(&myfile);
    
        QString text;
        QStringList split1;
    
        while(!out.atEnd())
        {
            text = out.readLine();
            split1.append(text.split(" "));
    
        }
        myfile.close();
    
        QFile myfile2("b.txt");
    
        if(myfile2.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            QTextStream out(&myfile2);
            out << split1.join("\n");
    
        }
        else
            QMessageBox::information(this," ", " - ");
    
        myfile2.close();
    

    and fully understood, that I just delete every space " " and separate words from lines. Pls help.

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

      Hi and welcome to devnet,

      If you only what the first word why not split1.append(text.split(" ").first()); ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi and welcome to devnet,

        If you only what the first word why not split1.append(text.split(" ").first()); ?

        A Offline
        A Offline
        Annatar
        wrote on last edited by
        #3

        @SGaist
        Oh. I didn`t notice this :)
        Thank you a lot.

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

          You're welcome !

          Since you have it working know, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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