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. QTemporaryFile - can't open
Forum Updated to NodeBB v4.3 + New Features

QTemporaryFile - can't open

Scheduled Pinned Locked Moved Unsolved General and Desktop
38 Posts 4 Posters 4.3k 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by qwe3
    #1

    Hi,
    I would like to create Temporary File, save text to it and read it later. That Temporary File has to be ".txt" file ( Windows 10 ). This is my code:

        QTemporaryFile *tmp = new QTemporaryFile;
        tmp->open();
        tmp->setFileName(tmpFile->fileName()+".txt");
        tmp->setAutoRemove(true);
        QProcess * proc = new QProcess;
        proc->start(here is cmdCommand: cmd /c "ipconfig > tmp->fileName()");
    
        QFile file(tmp->fileName());
        qDebug()<<file.open(QIODevice::ReadOnly);
        qDebug()<<file.errorString();
        qDebug()<<file.fileName();
    
        QTextStream ts(&file);
        while(!ts.atEnd())
        {
    
        }
        file.close();
        delete tmpFile;
    

    qDebug says:
    QFile::setFileName: File (C:/Users/myNameUser/AppData/Local/Temp/nameOfMyApp.CfKtTf) is already opened
    false
    "Can't find that file" //( this is my translation to English, I get this text in my language )
    "C:/Users/myNameUser/AppData/Local/Temp/nameOfMyApp.CfKtTf.txt"

    When I open "C:/Users/myNameUser/AppData/Local/Temp/nameOfMyApp.CfKtTf.txt"
    in Windows Explorer everything is ok ( I see the result of ipconfig ).

    So what is the problem?

    jsulmJ 1 Reply Last reply
    0
    • Q qwe3

      Hi,
      I would like to create Temporary File, save text to it and read it later. That Temporary File has to be ".txt" file ( Windows 10 ). This is my code:

          QTemporaryFile *tmp = new QTemporaryFile;
          tmp->open();
          tmp->setFileName(tmpFile->fileName()+".txt");
          tmp->setAutoRemove(true);
          QProcess * proc = new QProcess;
          proc->start(here is cmdCommand: cmd /c "ipconfig > tmp->fileName()");
      
          QFile file(tmp->fileName());
          qDebug()<<file.open(QIODevice::ReadOnly);
          qDebug()<<file.errorString();
          qDebug()<<file.fileName();
      
          QTextStream ts(&file);
          while(!ts.atEnd())
          {
      
          }
          file.close();
          delete tmpFile;
      

      qDebug says:
      QFile::setFileName: File (C:/Users/myNameUser/AppData/Local/Temp/nameOfMyApp.CfKtTf) is already opened
      false
      "Can't find that file" //( this is my translation to English, I get this text in my language )
      "C:/Users/myNameUser/AppData/Local/Temp/nameOfMyApp.CfKtTf.txt"

      When I open "C:/Users/myNameUser/AppData/Local/Temp/nameOfMyApp.CfKtTf.txt"
      in Windows Explorer everything is ok ( I see the result of ipconfig ).

      So what is the problem?

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

      @qwe3 Why do you call setFileName() after opening the file?
      Also you should rather use https://doc.qt.io/qt-5/qtemporaryfile.html#createNativeFile-1 to get a temp file name you need.

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

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        qwe3
        wrote on last edited by qwe3
        #3

        @jsulm Hmmm, when I try ( I delete open() and setFileName() ):

            QTemporaryFile *tmp= QTemporaryFile::createNativeFile("xyz.txt");
            tmp->setAutoRemove(true);
            QProcess * proc = new QProcess;
            THE REST
        

        my app is creashed.

        EDIT: Ok, that function don't create QTemporaryFile with given name. I would like to create only temporary File for the moment in C:/Users/myNameUser/AppData/Local/Temp/

        jsulmJ 1 Reply Last reply
        0
        • Q qwe3

          @jsulm Hmmm, when I try ( I delete open() and setFileName() ):

              QTemporaryFile *tmp= QTemporaryFile::createNativeFile("xyz.txt");
              tmp->setAutoRemove(true);
              QProcess * proc = new QProcess;
              THE REST
          

          my app is creashed.

          EDIT: Ok, that function don't create QTemporaryFile with given name. I would like to create only temporary File for the moment in C:/Users/myNameUser/AppData/Local/Temp/

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

          @qwe3 said in QTemporaryFile - can't open:

          my app is creashed

          where exactly?

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

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qwe3
            wrote on last edited by
            #5

            @jsulm I don;t have file xyz.txt on my pc

            jsulmJ 2 Replies Last reply
            0
            • Q qwe3

              @jsulm I don;t have file xyz.txt on my pc

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

              @qwe3 OK, forget about createNativeFile, it's not what you need.
              Just try to call setFileName BEFORE you try to open the file.

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

              1 Reply Last reply
              1
              • Q qwe3

                @jsulm I don;t have file xyz.txt on my pc

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

                @qwe3 But, actually I'm wondering why you want to set the file name? The problem is that such a file could already exist, that's why there is QTemporaryFile - it makes sure a unique temp file is created.

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

                1 Reply Last reply
                3
                • Q Offline
                  Q Offline
                  qwe3
                  wrote on last edited by
                  #8

                  @jsulm When I have:

                      QTemporaryFile *tmp = new QTemporaryFile;
                      tmp->setFileName(tmpFile->fileName()+".txt");
                      tmp->open();
                      tmp->setAutoRemove(true);
                  

                  and I check after that the fileName i get only ".txt".

                  Can I read from QTextStream when the file is not "txt"?

                  jsulmJ 1 Reply Last reply
                  0
                  • Q qwe3

                    @jsulm When I have:

                        QTemporaryFile *tmp = new QTemporaryFile;
                        tmp->setFileName(tmpFile->fileName()+".txt");
                        tmp->open();
                        tmp->setAutoRemove(true);
                    

                    and I check after that the fileName i get only ".txt".

                    Can I read from QTextStream when the file is not "txt"?

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

                    @qwe3 said in QTemporaryFile - can't open:

                    Can I read from QTextStream when the file is not "txt"?

                    Of course you can - file name extension does NOT define the file format and QTextStream does not care about any file name extensions. You can name your file what ever you like. You could for example name a JPEG file picture.txt - it would be still a JPEG image.

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

                    1 Reply Last reply
                    2
                    • Q Offline
                      Q Offline
                      qwe3
                      wrote on last edited by qwe3
                      #10

                      @jsulm When I have:

                          QTemporaryFile *tmp = new QTemporaryFile;
                          tmp->open();
                          tmp->setAutoRemove(true);
                      
                          QProcess
                      
                          QFile file(tmp->fileName());
                          qInfo()<<file.open(QIODevice::ReadOnly);
                          qInfo()<<file.errorString();
                          qInfo()<<file.fileName();
                          QTextStream ts(&file);
                          while(!ts.atEnd())
                         {
                            QString line = ts.readLine();
                            qDebug()<<"text";
                         }
                      

                      and I open that tmp->fileName() in windows explorer by notepad, it is empty. When I setFileName and add ".txt" it is not empty :D

                      Debug before while loop says:
                      true
                      "Unknown error"
                      "C:/Users/myUserName/AppData/Local/Temp/myAppName.nkUyod"

                      But I still don't see "text". It is in while loop.

                      jsulmJ 1 Reply Last reply
                      0
                      • Q qwe3

                        @jsulm When I have:

                            QTemporaryFile *tmp = new QTemporaryFile;
                            tmp->open();
                            tmp->setAutoRemove(true);
                        
                            QProcess
                        
                            QFile file(tmp->fileName());
                            qInfo()<<file.open(QIODevice::ReadOnly);
                            qInfo()<<file.errorString();
                            qInfo()<<file.fileName();
                            QTextStream ts(&file);
                            while(!ts.atEnd())
                           {
                              QString line = ts.readLine();
                              qDebug()<<"text";
                           }
                        

                        and I open that tmp->fileName() in windows explorer by notepad, it is empty. When I setFileName and add ".txt" it is not empty :D

                        Debug before while loop says:
                        true
                        "Unknown error"
                        "C:/Users/myUserName/AppData/Local/Temp/myAppName.nkUyod"

                        But I still don't see "text". It is in while loop.

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

                        @qwe3 said in QTemporaryFile - can't open:

                        QFile file(tmp->fileName());

                        There is no need for QFile as QTemporaryFile IS QFile.
                        Now you simply try to open a file which is already open.
                        Remove file and use tmp directly.

                        Also atEnd() will probably return true as your temp file is empty.

                        And what is this QProcess in your code snippet?

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

                        1 Reply Last reply
                        1
                        • Q Offline
                          Q Offline
                          qwe3
                          wrote on last edited by qwe3
                          #12

                          @jsulm Now my code is:

                              QTemporaryFile *tmp = new QTemporaryFile;
                              tmp->open();
                              tmp->setAutoRemove(true);
                          
                              QProcess *proc = new QProcess;
                              QString cmdCommand = R"(cmd /c "net use > )"+tmp->fileName()+R"(")";
                              proc->start(cmdCommand);
                              qDebug()<<tmp->open();
                              qDebug()<<tmp->size();
                          
                              QTextStream ts(tmp);
                              while(!ts.atEnd())
                              {
                          
                              }
                          

                          qDebug says:
                          true
                          0

                          I know that cmdCommand is good - when I add ".txt" to file, and open that file in Windows Explorer I see the result of that command.

                          jsulmJ 1 Reply Last reply
                          0
                          • Q qwe3

                            @jsulm Now my code is:

                                QTemporaryFile *tmp = new QTemporaryFile;
                                tmp->open();
                                tmp->setAutoRemove(true);
                            
                                QProcess *proc = new QProcess;
                                QString cmdCommand = R"(cmd /c "net use > )"+tmp->fileName()+R"(")";
                                proc->start(cmdCommand);
                                qDebug()<<tmp->open();
                                qDebug()<<tmp->size();
                            
                                QTextStream ts(tmp);
                                while(!ts.atEnd())
                                {
                            
                                }
                            

                            qDebug says:
                            true
                            0

                            I know that cmdCommand is good - when I add ".txt" to file, and open that file in Windows Explorer I see the result of that command.

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

                            @qwe3 Are you aware that you have to wait until the process has finished before you can access the content of the file? You can't just immediately read the file. And I doubt on Windows this will work as the process probably will not be able to write to this file as it is open in your app. You should call close() before proc->start(cmdCommand); and open again when the process finished.
                            Either use https://doc.qt.io/qt-5/qprocess.html#waitForFinished or https://doc.qt.io/qt-5/qprocess.html#finished

                            "when I add ".txt" to file, and open that file in Windows Explorer I see the result of that command" - it will work same way without adding .txt if you explicetly open that file in a text editor. Again: file extension does NOT influence the format of the data in the file. It only tells Windows explorer what application to use to pen it if you double click on it. If you don't believe me do this: change the file extension of a picture to .txt and double click on it...

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

                            1 Reply Last reply
                            0
                            • Q Offline
                              Q Offline
                              qwe3
                              wrote on last edited by qwe3
                              #14

                              @jsulm You are right. But...

                              I do something like:
                              tmp->fileName(), wait 10 seconds, don't delete tmp and open it in windows Explorer - I still see only empty File. Of course my app is still working.

                              "net use" is very simple command

                              jsulmJ 1 Reply Last reply
                              0
                              • Q qwe3

                                @jsulm You are right. But...

                                I do something like:
                                tmp->fileName(), wait 10 seconds, don't delete tmp and open it in windows Explorer - I still see only empty File. Of course my app is still working.

                                "net use" is very simple command

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

                                @qwe3 said in QTemporaryFile - can't open:

                                wait 10 seconds

                                How do you wait? Did you close the tmp file before starting the process as I suggested?
                                Also, if using QProcess please do proper error handling to see what happens: https://doc.qt.io/qt-5/qprocess.html#errorOccurred
                                https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError
                                https://doc.qt.io/qt-5/qprocess.html#error
                                https://doc.qt.io/qt-5/qprocess.html#exitCode

                                You actually do not need a file here at all to get the output of that command as you can use https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and https://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput

                                QProcess proc;
                                QString cmdCommand = "cmd";
                                QStringList param;
                                param << "/c" << "net" << "use";
                                proc.start(cmdCommand, param);
                                proc.waitForFinished();
                                QByteArray output = proc.readAllStandardOutput();
                                

                                Also, the way you're currently using QProcess is wrong: do not pass command AND parameter in a single string! See the code above for how it should be (and check documentation).

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

                                1 Reply Last reply
                                1
                                • Q Offline
                                  Q Offline
                                  qwe3
                                  wrote on last edited by
                                  #16

                                  @jsulm Of course I know readAllStandardOutput, but I have to write data to file and from file get data to QTextStream.
                                  Ok, so when I have code something like ( here I don't use temporary File - textFile5 isn't exists - so it's created ):

                                  #include "mainwindow.h"
                                  #include "ui_mainwindow.h"
                                  #include <QDebug>
                                  MainWindow::MainWindow(QWidget *parent)
                                      : QMainWindow(parent)
                                      , ui(new Ui::MainWindow)
                                  {
                                      ui->setupUi(this);
                                      QFile someFile(R"(C:\Users\myUserName\Desktop\textFile5)");
                                  
                                      tmpFile = new QTemporaryFile;
                                      tmpFile->open();
                                      proc = new QProcess;
                                      connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                      proc->start(R"(cmd /c "net use > )"+someFile.fileName()+R"(")");
                                      proc->waitForFinished();
                                  
                                      qInfo()<<proc->exitCode();
                                      tmpFile->close();
                                      qInfo()<<tmpFile->fileName();
                                  }
                                  
                                  MainWindow::~MainWindow()
                                  {
                                      delete ui;
                                  }
                                  
                                  void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                  {
                                      qInfo()<<error;
                                  }
                                  
                                  

                                  Everything is ok - I open in windows explorer C:\Users\myUserName\Desktop\textFile5 and there is a data and exitCode is 0.

                                  Now I change my code to:

                                  #include "mainwindow.h"
                                  #include "ui_mainwindow.h"
                                  #include <QDebug>
                                  MainWindow::MainWindow(QWidget *parent)
                                      : QMainWindow(parent)
                                      , ui(new Ui::MainWindow)
                                  {
                                      ui->setupUi(this);
                                      QFile someFile(R"(C:\Users\myUserName\Desktop\text5)");
                                  
                                      tmpFile = new QTemporaryFile;
                                      tmpFile->open();
                                      proc = new QProcess;
                                      connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                      proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                                      proc->waitForFinished();
                                  
                                      qInfo()<<proc->exitCode();
                                      tmpFile->close();
                                      qInfo()<<tmpFile->fileName();
                                  }
                                  
                                  MainWindow::~MainWindow()
                                  {
                                      delete ui;
                                  }
                                  
                                  void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                  {
                                      qInfo()<<error;
                                  }
                                  
                                  

                                  I only change one line: proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");

                                  Now exitCode is 1 ( so it's different ) and temporary file is empty.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • Q qwe3

                                    @jsulm Of course I know readAllStandardOutput, but I have to write data to file and from file get data to QTextStream.
                                    Ok, so when I have code something like ( here I don't use temporary File - textFile5 isn't exists - so it's created ):

                                    #include "mainwindow.h"
                                    #include "ui_mainwindow.h"
                                    #include <QDebug>
                                    MainWindow::MainWindow(QWidget *parent)
                                        : QMainWindow(parent)
                                        , ui(new Ui::MainWindow)
                                    {
                                        ui->setupUi(this);
                                        QFile someFile(R"(C:\Users\myUserName\Desktop\textFile5)");
                                    
                                        tmpFile = new QTemporaryFile;
                                        tmpFile->open();
                                        proc = new QProcess;
                                        connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                        proc->start(R"(cmd /c "net use > )"+someFile.fileName()+R"(")");
                                        proc->waitForFinished();
                                    
                                        qInfo()<<proc->exitCode();
                                        tmpFile->close();
                                        qInfo()<<tmpFile->fileName();
                                    }
                                    
                                    MainWindow::~MainWindow()
                                    {
                                        delete ui;
                                    }
                                    
                                    void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                    {
                                        qInfo()<<error;
                                    }
                                    
                                    

                                    Everything is ok - I open in windows explorer C:\Users\myUserName\Desktop\textFile5 and there is a data and exitCode is 0.

                                    Now I change my code to:

                                    #include "mainwindow.h"
                                    #include "ui_mainwindow.h"
                                    #include <QDebug>
                                    MainWindow::MainWindow(QWidget *parent)
                                        : QMainWindow(parent)
                                        , ui(new Ui::MainWindow)
                                    {
                                        ui->setupUi(this);
                                        QFile someFile(R"(C:\Users\myUserName\Desktop\text5)");
                                    
                                        tmpFile = new QTemporaryFile;
                                        tmpFile->open();
                                        proc = new QProcess;
                                        connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                        proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                                        proc->waitForFinished();
                                    
                                        qInfo()<<proc->exitCode();
                                        tmpFile->close();
                                        qInfo()<<tmpFile->fileName();
                                    }
                                    
                                    MainWindow::~MainWindow()
                                    {
                                        delete ui;
                                    }
                                    
                                    void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                    {
                                        qInfo()<<error;
                                    }
                                    
                                    

                                    I only change one line: proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");

                                    Now exitCode is 1 ( so it's different ) and temporary file is empty.

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

                                    @qwe3 said in QTemporaryFile - can't open:

                                    Now exitCode is 1 ( so it's different )

                                    That's why I suggested to implement proper error handling...
                                    And I think this is now third time I write it: close the temp file before starting process!

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

                                    1 Reply Last reply
                                    1
                                    • Q Offline
                                      Q Offline
                                      qwe3
                                      wrote on last edited by
                                      #18

                                      @jsulm Ok, I close temp file before starting process.

                                      #include "mainwindow.h"
                                      #include "ui_mainwindow.h"
                                      #include <QDebug>
                                      MainWindow::MainWindow(QWidget *parent)
                                          : QMainWindow(parent)
                                          , ui(new Ui::MainWindow)
                                      {
                                          ui->setupUi(this);
                                      
                                          tmpFile = new QTemporaryFile;
                                          tmpFile->open();
                                          proc = new QProcess;
                                          tmpFile->close();
                                          connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                          connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                                          proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                                          proc->waitForFinished();
                                      
                                          qInfo()<<proc->exitCode();
                                      
                                          qInfo()<<tmpFile->fileName();
                                      }
                                      
                                      MainWindow::~MainWindow()
                                      {
                                          delete ui;
                                      }
                                      
                                      void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                      {
                                          qInfo()<<"error"<<error;
                                      }
                                      
                                      void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                                      {
                                          qInfo()<<"exitCode in Slot"<<exitCode;
                                          qInfo()<<"exit Status in Slot"<<exitStatus;
                                      }
                                      
                                      

                                      qInfo says:
                                      exitCode in Slot 1
                                      exit Status in Slot QProcess::NormalExit
                                      1

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • Q qwe3

                                        @jsulm Ok, I close temp file before starting process.

                                        #include "mainwindow.h"
                                        #include "ui_mainwindow.h"
                                        #include <QDebug>
                                        MainWindow::MainWindow(QWidget *parent)
                                            : QMainWindow(parent)
                                            , ui(new Ui::MainWindow)
                                        {
                                            ui->setupUi(this);
                                        
                                            tmpFile = new QTemporaryFile;
                                            tmpFile->open();
                                            proc = new QProcess;
                                            tmpFile->close();
                                            connect(proc, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(errorOccurredSlot(QProcess::ProcessError)));
                                            connect(proc, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finishedSlot(int , QProcess::ExitStatus )));
                                            proc->start(R"(cmd /c "net use > )"+tmpFile->fileName()+R"(")");
                                            proc->waitForFinished();
                                        
                                            qInfo()<<proc->exitCode();
                                        
                                            qInfo()<<tmpFile->fileName();
                                        }
                                        
                                        MainWindow::~MainWindow()
                                        {
                                            delete ui;
                                        }
                                        
                                        void MainWindow::errorOccurredSlot(QProcess::ProcessError error)
                                        {
                                            qInfo()<<"error"<<error;
                                        }
                                        
                                        void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus exitStatus)
                                        {
                                            qInfo()<<"exitCode in Slot"<<exitCode;
                                            qInfo()<<"exit Status in Slot"<<exitStatus;
                                        }
                                        
                                        

                                        qInfo says:
                                        exitCode in Slot 1
                                        exit Status in Slot QProcess::NormalExit
                                        1

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

                                        @qwe3 "but I have to write data to file and from file get data to QTextStream" - you do not need a file to use QTextStream, I still don't understand why you need a file... It would be way easier to get the output from the command without messing up with temp file.

                                        QProcess proc;
                                        QString cmdCommand = "cmd";
                                        QStringList param;
                                        param << "/c" << "net" << "use";
                                        proc.start(cmdCommand, param);
                                        proc.waitForFinished();
                                        QTextStream stream(proc.readAllStandardOutput());
                                        

                                        Does your current code write the file or not?

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

                                        1 Reply Last reply
                                        2
                                        • Q Offline
                                          Q Offline
                                          qwe3
                                          wrote on last edited by qwe3
                                          #20

                                          @jsulm Ok, so the problem is that I can't use QProcess. I have to use winApi CreateProcess, which is a little different. And in this CreateProcess the new process will be have shell parent, so this is very strange. I know. That new process will be execute cmdCommand. I know that can be problem with ( waitForFinished, but at this moment is not a problem ). So I can't use readAllStandardOutput. I have to write data to File. This is only way. So now i would like know why this code with QTemporaryFile and QProcess doesn't work. Of course QProcess works when I use it with file which is on the Desktop or will be created.

                                          I tested CreateProcess from WinApi and it works, but I have to use QTemporaryFile. So I started from QTemporaryFile and QProcess.

                                          EDIT my last code which is here: there is no data on temporary File.

                                          jsulmJ 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