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 save any string in text file in Qt ?
Forum Updated to NodeBB v4.3 + New Features

How to save any string in text file in Qt ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 908 Views
  • 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.
  • P Offline
    P Offline
    Pappu Kumar Keshari
    wrote on last edited by
    #1

    If I fetched data in (QString str ) str then I have to copy all the data which is in str to Copy.txt(Text File) file with file location. How Can I do it ?

    jsulmJ 1 Reply Last reply
    0
    • P Pappu Kumar Keshari

      If I fetched data in (QString str ) str then I have to copy all the data which is in str to Copy.txt(Text File) file with file location. How Can I do it ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Pappu-Kumar-Keshari Please start here:

      • https://doc.qt.io/qt-6/qfile.html
      • https://doc.qt.io/qt-6/qtextstream.html - here you can even find example code for your use case

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • SavizS Offline
        SavizS Offline
        Saviz
        wrote on last edited by
        #3

        If you are attempting to store your string data in a file, then you can probably look at this example:

        void save_to_file(QString filePath, QString data)
        {
            if(filePath.isEmpty())
            {
                qDebug() << "File path is empty!"
        
                return;
            }
        
            QFile fileObject(filePath);
        
            if(!fileObject.open(QIODevice::WriteOnly))
            {
                qDebug() << "Error! could not open file."
        
                return;
            }
        
            QTextStream stream(&fileObject);
        
            stream << data;
        
            fileObject.close();
        }
        

        Hope this helps.

        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