How to creating new file to designated path
-
wrote on 31 Oct 2018, 10:27 last edited by victor wang
Hi All,
I'm using linux system.
Now I want to create a file in my USB directory.
I was trying to see if QFile can do what I want.
But it seems can only create file in root path or home path.Is there any way to create a new file to where I designated?
Thanks in Advanced!
-
Hi
QString filename = "/run/media/usb/test.txt"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << "something" << endl; } else qDebug << "file open error", }
wrote on 1 Nov 2018, 10:09 last edited by victor wang 11 Jan 2018, 10:10@mrjj said in How to creating new file to designated path:
Hi
QString filename = "/run/media/usb/test.txt"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << "something" << endl; } else qDebug << "file open error", }
Thank you so much for your example!
I just google for the answer here.
https://stackoverflow.com/questions/3024382/how-to-create-a-new-file-with-full-path-in-qtIt explains that QFile will check if your file exist or not, if not it will create a new one then open it!
Here is my new code.
int CreateFile::create(QString filePath) { QFile file; file.setFileName(filePath); if(file.open(QIODevice::WriteOnly|QIODevice::Text)) { qDebug("Create "+filePath+" Success!"); } else { qDebug("Create "+filePath+" Filed!"); return false; } file.close(); if(file.exists()) { qDebug(filePath+"exist"); return true; } else return false; }
For additional question, are there any ways to report the error from system when file could not normally be created?
-
Hi
If you mean you want to create any folder in the path given that do not exists then no
as far as i know.
But you can use
http://doc.qt.io/qt-5/qdir.html#mkpath
to make the full path and then QFile to save the file. -
Hi All,
I'm using linux system.
Now I want to create a file in my USB directory.
I was trying to see if QFile can do what I want.
But it seems can only create file in root path or home path.Is there any way to create a new file to where I designated?
Thanks in Advanced!
@victor-wang said in How to creating new file to designated path:
But it seems can only create file in root path or home path.
That's wrong, why do you think so? You can store files wherever you have write access. Just give QFile the path.
What is your actual problem?
USB storage is usually mounted in /media/USB_DEVICE_NAME on Linux. -
@victor-wang said in How to creating new file to designated path:
But it seems can only create file in root path or home path.
That's wrong, why do you think so? You can store files wherever you have write access. Just give QFile the path.
What is your actual problem?
USB storage is usually mounted in /media/USB_DEVICE_NAME on Linux.wrote on 1 Nov 2018, 08:38 last edited by@jsulm said in How to creating new file to designated path:
@victor-wang said in How to creating new file to designated path:
But it seems can only create file in root path or home path.
That's wrong, why do you think so? You can store files wherever you have write access. Just give QFile the path.
What is your actual problem?
USB storage is usually mounted in /media/USB_DEVICE_NAME on Linux.Hi,
Thanks for your reply.
Here is my current code.QFile filepath; filepath.setFileName("/run/media/usb"); if(filepath.exist()) { qDebug("file is exist"); } else { qDebug("create new file"); }
Now I could not know which I have to use.
Below command is to create a Folder.filepath.mkdir()
But I want to create a File.
I do not know which function that I can use to create a file not folder.can you teach me which one I should use?
-
Hi
If you mean you want to create any folder in the path given that do not exists then no
as far as i know.
But you can use
http://doc.qt.io/qt-5/qdir.html#mkpath
to make the full path and then QFile to save the file.wrote on 1 Nov 2018, 08:42 last edited by@mrjj said in How to creating new file to designated path:
Hi
If you mean you want to create any folder in the path given that do not exists then no
as far as i know.
But you can use
http://doc.qt.io/qt-5/qdir.html#mkpath
to make the full path and then QFile to save the file.Actually I want to create a File that does not exist under the exist path.
For instance, there is a folder "/run/media/usb" and I want to create a file that does not exist and call it test.txt.I'm not sure that QFile can complete it or not.
Cause there is no test.txt then QFile could not open it. -
Hi
QString filename = "/run/media/usb/test.txt"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << "something" << endl; } else qDebug << "file open error", }
-
a slight additon to @mrjj
IIRC than QFile can not create subdirectories. So make sure the target folder actually exists, and if it doesn't use mkdir beforehand. -
a slight additon to @mrjj
IIRC than QFile can not create subdirectories. So make sure the target folder actually exists, and if it doesn't use mkdir beforehand.@J.Hilk I'm pretty sure these restrictions apply to every concept of file. It's like a car that can only drive on a road that already exists.
-
Hi
QString filename = "/run/media/usb/test.txt"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << "something" << endl; } else qDebug << "file open error", }
wrote on 1 Nov 2018, 10:09 last edited by victor wang 11 Jan 2018, 10:10@mrjj said in How to creating new file to designated path:
Hi
QString filename = "/run/media/usb/test.txt"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << "something" << endl; } else qDebug << "file open error", }
Thank you so much for your example!
I just google for the answer here.
https://stackoverflow.com/questions/3024382/how-to-create-a-new-file-with-full-path-in-qtIt explains that QFile will check if your file exist or not, if not it will create a new one then open it!
Here is my new code.
int CreateFile::create(QString filePath) { QFile file; file.setFileName(filePath); if(file.open(QIODevice::WriteOnly|QIODevice::Text)) { qDebug("Create "+filePath+" Success!"); } else { qDebug("Create "+filePath+" Filed!"); return false; } file.close(); if(file.exists()) { qDebug(filePath+"exist"); return true; } else return false; }
For additional question, are there any ways to report the error from system when file could not normally be created?
-
@mrjj said in How to creating new file to designated path:
Hi
QString filename = "/run/media/usb/test.txt"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << "something" << endl; } else qDebug << "file open error", }
Thank you so much for your example!
I just google for the answer here.
https://stackoverflow.com/questions/3024382/how-to-create-a-new-file-with-full-path-in-qtIt explains that QFile will check if your file exist or not, if not it will create a new one then open it!
Here is my new code.
int CreateFile::create(QString filePath) { QFile file; file.setFileName(filePath); if(file.open(QIODevice::WriteOnly|QIODevice::Text)) { qDebug("Create "+filePath+" Success!"); } else { qDebug("Create "+filePath+" Filed!"); return false; } file.close(); if(file.exists()) { qDebug(filePath+"exist"); return true; } else return false; }
For additional question, are there any ways to report the error from system when file could not normally be created?
-
wrote on 2 Nov 2018, 03:27 last edited by
@mrjj said in How to creating new file to designated path:
@victor-wang
Hi
there is
http://doc.qt.io/qt-5/qfiledevice.html#errorThanks alot !
I will look into it!
1/11