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. Cannot write to file in qt
Forum Updated to NodeBB v4.3 + New Features

Cannot write to file in qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 619 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.
  • S Offline
    S Offline
    sam12
    wrote on 11 Dec 2021, 04:20 last edited by
    #1

    void fileReceivefromServer()
    {
    char filename[256];
    sockaddr_in p;
    char rec[50] = "";
    QFile file;
    // QFileDevice x;
    int size;
    char bb[2];
    char bf2[100];

        p = readFromSocketWithBlock(filename, 32);
        qDebug()<<"filename: " << filename <<Qt::endl ;
    
        // read size of file
        p = readFromSocketWithBlock(rec, sizeof(rec));
        size = atoi(rec);
        qDebug()<<"size: " << size <<Qt::endl ;
    
        string v(filename);
        QString filepath(("C:/Users/hp/OneDrive/Documents/PrD/"+v).c_str());
        qDebug()<<"filepath: " << filepath <<Qt::endl ;
        file.setFileName(filepath);
    
        // Check if file is opened
        if (!file.open(QIODevice::ReadWrite))
        {
            qDebug()<< "Could not open file ";
        }
        else
            qDebug() << "file opened";
    
       // QTextStream out(&file);
    
        QByteArray arr;
        QString filecontent="";
         
    
        for (int i = size; i>0 ; i--)
        {
    
            memset(bb, 0, 2);
            readFromSocketWithBlock(bb, sizeof(bb));
            QString ty(bb);
            filecontent+=ty;
         
    
    
    
        }
    
    
        filecontent+="\00";
        arr.fromStdString(filecontent.toStdString());
        
        file.openMode();
        file.setTextModeEnabled(true);
        if (int u =file.write(arr) > 0)
        {
          qDebug()<<"Wrote to file ";
        }
        else qDebug()<<"can't write to file ";  ;
    
        qDebug()<<"W3 \n";
    
        file.close();
      
    }
    

    };

    J J 2 Replies Last reply 11 Dec 2021, 08:52
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 11 Dec 2021, 04:49 last edited by
      #2

      @sam12 You need to describe the symptoms you are seeing because we cannot guess them.

      S 1 Reply Last reply 11 Dec 2021, 17:02
      1
      • S sam12
        11 Dec 2021, 04:20

        void fileReceivefromServer()
        {
        char filename[256];
        sockaddr_in p;
        char rec[50] = "";
        QFile file;
        // QFileDevice x;
        int size;
        char bb[2];
        char bf2[100];

            p = readFromSocketWithBlock(filename, 32);
            qDebug()<<"filename: " << filename <<Qt::endl ;
        
            // read size of file
            p = readFromSocketWithBlock(rec, sizeof(rec));
            size = atoi(rec);
            qDebug()<<"size: " << size <<Qt::endl ;
        
            string v(filename);
            QString filepath(("C:/Users/hp/OneDrive/Documents/PrD/"+v).c_str());
            qDebug()<<"filepath: " << filepath <<Qt::endl ;
            file.setFileName(filepath);
        
            // Check if file is opened
            if (!file.open(QIODevice::ReadWrite))
            {
                qDebug()<< "Could not open file ";
            }
            else
                qDebug() << "file opened";
        
           // QTextStream out(&file);
        
            QByteArray arr;
            QString filecontent="";
             
        
            for (int i = size; i>0 ; i--)
            {
        
                memset(bb, 0, 2);
                readFromSocketWithBlock(bb, sizeof(bb));
                QString ty(bb);
                filecontent+=ty;
             
        
        
        
            }
        
        
            filecontent+="\00";
            arr.fromStdString(filecontent.toStdString());
            
            file.openMode();
            file.setTextModeEnabled(true);
            if (int u =file.write(arr) > 0)
            {
              qDebug()<<"Wrote to file ";
            }
            else qDebug()<<"can't write to file ";  ;
        
            qDebug()<<"W3 \n";
        
            file.close();
          
        }
        

        };

        J Offline
        J Offline
        JonB
        wrote on 11 Dec 2021, 08:52 last edited by JonB 12 Dec 2021, 07:58
        #3

        @sam12
        Hello and welcome.

        As @ChrisW67 said, this is not a question, it is a paste of some code. If you have a question you must state what it is. If it is not doing what you want/expect, you must state what it does/does not do and how that compares with whatever you expected.

        I don't mean to be rude, but your code is a terrible mishmash of C, C++, std:: and Qt. You really should tidy it up and use things consistently. These days there is no longer any need to use C-type constructs, char []s, memset(), and half the other stuff.

        Your statement

        if (int u =file.write(arr) > 0)
        

        is not good, and does not do what you (probably) think it does.

        Another problem comes from

        arr.fromStdString(filecontent.toStdString());
        

        What do you think this does? If you think it sets arr it does not. Consequently your arr remains empty --- test for that immediately above your file.write(arr) statement.

        Finally I don't think your opening your file for ReadWrite and then writing to it does what you might be expecting it to do. If that file already exists before you open it, it may not have the content you expect after you have finished writing to it.

        1 Reply Last reply
        0
        • C ChrisW67
          11 Dec 2021, 04:49

          @sam12 You need to describe the symptoms you are seeing because we cannot guess them.

          S Offline
          S Offline
          sam12
          wrote on 11 Dec 2021, 17:02 last edited by sam12 12 Nov 2021, 17:04
          #4

          @ChrisW67 Whenever I try to write to the file at the file.write() I get an error which states the inferior stopped because it received a signal from the operating system (Signal meaning. segmentation fault). The file exists and opens however when it writes the program crashes. I can't tell where the problem is I've been trying to debug and that's why my code doesn't seem clean and proper. I apologize for this. If you can help me figure out what can be wrong I will really appreciate it

          C 1 Reply Last reply 12 Dec 2021, 03:37
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 11 Dec 2021, 19:36 last edited by
            #5

            Simply use a debugger and see where it crashes exactly.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply 12 Dec 2021, 07:10
            1
            • S sam12
              11 Dec 2021, 17:02

              @ChrisW67 Whenever I try to write to the file at the file.write() I get an error which states the inferior stopped because it received a signal from the operating system (Signal meaning. segmentation fault). The file exists and opens however when it writes the program crashes. I can't tell where the problem is I've been trying to debug and that's why my code doesn't seem clean and proper. I apologize for this. If you can help me figure out what can be wrong I will really appreciate it

              C Offline
              C Offline
              ChrisW67
              wrote on 12 Dec 2021, 03:37 last edited by
              #6

              @JonB has already pointed out that this:

              arr.fromStdString(filecontent.toStdString());
              

              does not do what you think it does. QByteArray::fromStdString() is a static function that returns a QByteArray.. and you are discarding the return value. Nevertheless, arr should remain a valid, empty QByteArray. Then you do this:

              if (int u =file.write(arr) > 0)
              

              This time you try to capture the return value. I suggest you look at the prototype for QFile::write() (actually QIODevice::write()). Ask yourself:

              1. What type does this function return and where am I storing it?
              2. What warning message(s) did my compiler issue when it compiled this line?
              3. Are there other problems with this line?
              1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher
                11 Dec 2021, 19:36

                Simply use a debugger and see where it crashes exactly.

                S Offline
                S Offline
                sam12
                wrote on 12 Dec 2021, 07:10 last edited by
                #7

                @Christian-Ehrlicher I did it crashes exactly at File.write

                1 Reply Last reply
                0
                • S sam12
                  11 Dec 2021, 04:20

                  void fileReceivefromServer()
                  {
                  char filename[256];
                  sockaddr_in p;
                  char rec[50] = "";
                  QFile file;
                  // QFileDevice x;
                  int size;
                  char bb[2];
                  char bf2[100];

                      p = readFromSocketWithBlock(filename, 32);
                      qDebug()<<"filename: " << filename <<Qt::endl ;
                  
                      // read size of file
                      p = readFromSocketWithBlock(rec, sizeof(rec));
                      size = atoi(rec);
                      qDebug()<<"size: " << size <<Qt::endl ;
                  
                      string v(filename);
                      QString filepath(("C:/Users/hp/OneDrive/Documents/PrD/"+v).c_str());
                      qDebug()<<"filepath: " << filepath <<Qt::endl ;
                      file.setFileName(filepath);
                  
                      // Check if file is opened
                      if (!file.open(QIODevice::ReadWrite))
                      {
                          qDebug()<< "Could not open file ";
                      }
                      else
                          qDebug() << "file opened";
                  
                     // QTextStream out(&file);
                  
                      QByteArray arr;
                      QString filecontent="";
                       
                  
                      for (int i = size; i>0 ; i--)
                      {
                  
                          memset(bb, 0, 2);
                          readFromSocketWithBlock(bb, sizeof(bb));
                          QString ty(bb);
                          filecontent+=ty;
                       
                  
                  
                  
                      }
                  
                  
                      filecontent+="\00";
                      arr.fromStdString(filecontent.toStdString());
                      
                      file.openMode();
                      file.setTextModeEnabled(true);
                      if (int u =file.write(arr) > 0)
                      {
                        qDebug()<<"Wrote to file ";
                      }
                      else qDebug()<<"can't write to file ";  ;
                  
                      qDebug()<<"W3 \n";
                  
                      file.close();
                    
                  }
                  

                  };

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 13 Dec 2021, 06:39 last edited by
                  #8

                  @sam12 said in Cannot write to file in qt:

                  // Check if file is opened

                  You don't care whether the file was opened or not.
                  So, was the file opened before you wrote into it?

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

                  J 1 Reply Last reply 13 Dec 2021, 08:38
                  0
                  • J jsulm
                    13 Dec 2021, 06:39

                    @sam12 said in Cannot write to file in qt:

                    // Check if file is opened

                    You don't care whether the file was opened or not.
                    So, was the file opened before you wrote into it?

                    J Offline
                    J Offline
                    JonB
                    wrote on 13 Dec 2021, 08:38 last edited by JonB
                    #9

                    @jsulm
                    OP's code outputs a message to say if file opened or not. I cannot believe OP would complain about later failure to write to file if the message had indicated the open had failed, and would have told us so, surely....

                    1 Reply Last reply
                    0

                    1/9

                    11 Dec 2021, 04:20

                    • Login

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