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. File writing in pendrive
Forum Updated to NodeBB v4.3 + New Features

File writing in pendrive

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 780 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.
  • ashajgA Offline
    ashajgA Offline
    ashajg
    wrote on last edited by aha_1980
    #1

    Hi

    I am writing data in a text file present in a pendrive. I am successful in writing the data but I want to handle a case if pendrive is removed during data transfer I want to notify or I want to do an error handling in such case. How can I do this

    code:

    QDir pathDir("/media/jBuild/USBSTORAGE");
    if(pathDir.exists())
    {
    QFile file("/media/jBuild/USBSTORAGE/out.txt")
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))return;
    QTextStream out(&file);
    out<<"...………………………………………………………………………………..";
    } 
    else
    {
    qDebug()<<"path doesnot exist!!";
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      well you can check
      https://doc-snapshots.qt.io/qt5-5.12/qtextstream.html#status
      for
      QTextStream::WriteFailed

      ashajgA 1 Reply Last reply
      1
      • mrjjM mrjj

        hi
        well you can check
        https://doc-snapshots.qt.io/qt5-5.12/qtextstream.html#status
        for
        QTextStream::WriteFailed

        ashajgA Offline
        ashajgA Offline
        ashajg
        wrote on last edited by
        #3

        Hi @mrjj

        Thanks for help!!

        But Even if I am removing pendrive I am getting status as 0. i.e it is indicating "The text stream is operating normally."

        CODE:

        QDir pathDir("/media/jBuild/USBSTORAGE");
        if(pathDir.exists())
        {
        QFile file("/media/jBuild/USBSTORAGE/out.txt")
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text))return;
        QTextStream out(&file);
        for(int i=0;i<100;i++)
        {
        QThread::msleep(1000);
        qDebug()<<"STATUS"<<out.status();
        out<<"the Magic num is"<<49<<"\n";
        }
        } 
        else
        {
        qDebug()<<"path doesnot exist!!";
        }
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          oh
          so on linux it wont detect usb being removed :)
          well you can use some of the write function of QFile. they return
          the bytes written and maybe you can detect that way ifs not going well.

          ashajgA 1 Reply Last reply
          1
          • mrjjM mrjj

            oh
            so on linux it wont detect usb being removed :)
            well you can use some of the write function of QFile. they return
            the bytes written and maybe you can detect that way ifs not going well.

            ashajgA Offline
            ashajgA Offline
            ashajg
            wrote on last edited by
            #5

            @mrjj What kind of write function I should write ….I mean I am new to this concept. Can you please explain me?

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              QFile has functions to write data also besides the <<
              https://doc.qt.io/qt-5/qiodevice.html#write
              that will return how many you wrote and -1 if error
              check if it gives error if usb is removed.
              it might not as linux is very odd with USB.

              ashajgA 1 Reply Last reply
              2
              • mrjjM mrjj

                Hi
                QFile has functions to write data also besides the <<
                https://doc.qt.io/qt-5/qiodevice.html#write
                that will return how many you wrote and -1 if error
                check if it gives error if usb is removed.
                it might not as linux is very odd with USB.

                ashajgA Offline
                ashajgA Offline
                ashajg
                wrote on last edited by
                #7

                @mrjj ok Thank you I will try this.

                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