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. Opening txt files in qt c++ and writing to it.

Opening txt files in qt c++ and writing to it.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.7k 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.
  • A Offline
    A Offline
    Alfie Anil
    wrote on last edited by
    #1

    Hello everyone,
    I'm having problems writing to files in qt. I have tried using QFile and using fstream but both seem to have problems. The problem is that although I think the code is right, the file isn't being written to at the location of the project, rather it is being written in the build directory. I tried changing the build directory to the location of the active project but that too doesn't seem to do anything.
    Any help is greatly appreciated.
    Thanks in advance

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      Hi,
      please paste the code you use and what do you think it should do? I am happy to take a look.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Alfie Anil
        wrote on last edited by
        #3

        Hi, this is the code:

        QString url;
                QFile file("urlfile.txt");
                file.open(QIODevice::ReadOnly | QIODevice::WriteOnly | QIODevice::Text);
                QTextStream txtStream(&file);
        
                txtStream << "Hello";
                qDebug() << "OK";
        
                file.close();
        

        I want it to create the txt file in the same location as the directory that the project is saved in, because afterwards I call a python exe file, which needs to open the file. Hope you understand thanks

        artwawA JonBJ 2 Replies Last reply
        0
        • A Alfie Anil

          Hi, this is the code:

          QString url;
                  QFile file("urlfile.txt");
                  file.open(QIODevice::ReadOnly | QIODevice::WriteOnly | QIODevice::Text);
                  QTextStream txtStream(&file);
          
                  txtStream << "Hello";
                  qDebug() << "OK";
          
                  file.close();
          

          I want it to create the txt file in the same location as the directory that the project is saved in, because afterwards I call a python exe file, which needs to open the file. Hope you understand thanks

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @Alfie-Anil Why ReadOnly | WriteOnly? Use just one please (I guess in your case WriteOnly).

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          2
          • A Alfie Anil

            Hi, this is the code:

            QString url;
                    QFile file("urlfile.txt");
                    file.open(QIODevice::ReadOnly | QIODevice::WriteOnly | QIODevice::Text);
                    QTextStream txtStream(&file);
            
                    txtStream << "Hello";
                    qDebug() << "OK";
            
                    file.close();
            

            I want it to create the txt file in the same location as the directory that the project is saved in, because afterwards I call a python exe file, which needs to open the file. Hope you understand thanks

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @Alfie-Anil said in Opening txt files in qt c++ and writing to it.:

            QFile file("urlfile.txt");

            This file will be relative to whatever the current directory happens to be in your application at the time the line is executed. Which may or may not be correct.

            Safer would be to use an absolute path.

            the same location as the directory that the project is saved in

            I don't know what "the directory that the project is saved in" is. There will not be any Qt methods to retrieve such a thing. There will be for, say, where the executable is being run from.

            Writing to such a location at runtime is not a good idea anyway. Look through https://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum to find a suitable one for where you want to save the file, and use that to build the absolute path.

            1 Reply Last reply
            5

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved