Clean File
-
Hi and welcome to the forums.
Im not 100% sure i understand what you ask.
You want to erase a file when you click the button or do you ask
if it will create an empty file, from the code shown? -
Hi and welcome to the forums.
Im not 100% sure i understand what you ask.
You want to erase a file when you click the button or do you ask
if it will create an empty file, from the code shown? -
@Bruno10x
Hi
If you mean erase like in delete it, there is
http://doc.qt.io/qt-5/qfile.html#remove-1Notice this is a static function
so you just call it likeQFile::remove( fullpathtofile.txt);
you dont need a QFile variable.
-
@Bruno10x
Hi
If you mean erase like in delete it, there is
http://doc.qt.io/qt-5/qfile.html#remove-1Notice this is a static function
so you just call it likeQFile::remove( fullpathtofile.txt);
you dont need a QFile variable.
-
@Bruno10x said in Clean File:
Ok, if I just want erase the content in file, How I do?
Open the file, read the fileSize and then do a QFile::write(QByteArray(0, filesize))
-
@Bruno10x said in Clean File:
Ok, if I just want erase the content in file, How I do?
Open the file, read the fileSize and then do a QFile::write(QByteArray(0, filesize))
You may have taken @Bruno10x's
Ok, if I just want erase the content in file,
a bit too literally :)
For his benefit: if by "erase content" you mean keep it same size but fill it with all bytes of value
0then @Christian-Ehrlicher's solution is what you need. [Clarification: provided you know your file is not too large... (else do it in a loop with a buffer's-worth at a time)]If you mean reset it to have length of zero, then your existing
file.open(QIODevice::WriteOnly)should already be accomplishing that? [Clarification: http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enumQIODevice::WriteOnly 0x0002 The device is open for writing. Note that this mode implies Truncate.
]