File writing in pendrive
-
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!!"; }
-
hi
well you can check
https://doc-snapshots.qt.io/qt5-5.12/qtextstream.html#status
for
QTextStream::WriteFailed -
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!!"; }
-
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. -
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.