how to create file if it not exist at give path in qt ?
-
@mpergand Hi QFile::exists will give information that file is exist or not. but how to create it when it's not there ?
@Qt-embedded-developer @mpergand gave you a socond link: what's wrong with it?
-
@Qt-embedded-developer @mpergand gave you a socond link: what's wrong with it?
@jsulm can you tell me with below written code
QFile file("out.txt");
file.open(QIODevice::ReadWrite)the file gets created or not ?
because before reading or writing i have to create the file and this information is i think not clearly get from mentioned link.
-
@jsulm can you tell me with below written code
QFile file("out.txt");
file.open(QIODevice::ReadWrite)the file gets created or not ?
because before reading or writing i have to create the file and this information is i think not clearly get from mentioned link.
@Qt-embedded-developer Why don't you read documentation? It answers your question (https://doc.qt.io/qt-5/qfile.html#open)...
-
@Qt-embedded-developer Why don't you read documentation? It answers your question (https://doc.qt.io/qt-5/qfile.html#open)...
This post is deleted! -
This post is deleted!
@Qt-embedded-developer Thats not true:
Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
This is from the posted link
-
This post is deleted!
@Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:
but for QIODevice::ReadWrite with this mode what happens is not mentioned
I guess you mean ReadOnly? Well, if the documentation says the file will be created in case of WriteOnly and ReadWrite, then isn't it logical that the file will not be created in all other modes? Also, this is really easy to test.
-
@Qt-embedded-developer Thats not true:
Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
This is from the posted link
@DerReisende said in how to create file if it not exist at give path in qt ?:
Thats not true
Really? How did you prove it is not true?
-
@DerReisende said in how to create file if it not exist at give path in qt ?:
Thats not true
Really? How did you prove it is not true?
@jsulm I was answering a deleted post from OP not yours.
-
@jsulm I was answering a deleted post from OP not yours.
@DerReisende Oh, sorry I was thinking I was replying to OP.
-
can anybody tell me which option i need to select for writing or updating into file which is not exist at given path. Because there may be situation when file may be not exist initially.
I want to know which option i need to choose for create file if it does not exist ?
i also want to know which option i need to choose for read and write combinedly using QFILE ?
@Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:
i also want to know which option i need to choose for read and write combinedly using QFILE ?
I will say this: most of the time when people here open a
QFile
withQIODevice::ReadWrite
it is unnecessary/wrong. What will you actually be doing which requires the file to be opened for read and write at the same time? -
@Qt-embedded-developer Thats not true:
Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
This is from the posted link
@jsulm means for first time if i want to create file then i must need to use WriteOnly or ReadWrite mode .
Okay thanks all. and sorry for deleting comment of mine before waiting for answer or updating the comment.
-
@Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:
i also want to know which option i need to choose for read and write combinedly using QFILE ?
I will say this: most of the time when people here open a
QFile
withQIODevice::ReadWrite
it is unnecessary/wrong. What will you actually be doing which requires the file to be opened for read and write at the same time?@JonB can you elaborate some more. because in my case when i write any thing in file then
1] After first time i have to read file
2] i have to add update the content of file
3] then why this QIODevice::ReadWrite mode is unnecessary ? -
@JonB can you elaborate some more. because in my case when i write any thing in file then
1] After first time i have to read file
2] i have to add update the content of file
3] then why this QIODevice::ReadWrite mode is unnecessary ?@Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:
then why this QIODevice::ReadWrite mode is unnecessary ?
It is necessary if you want to create a new file and write to it.
But if you only want to read existing file or to only write into a file use ReadOnly/WriteOnly. -
@JonB can you elaborate some more. because in my case when i write any thing in file then
1] After first time i have to read file
2] i have to add update the content of file
3] then why this QIODevice::ReadWrite mode is unnecessary ?@Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:
i have to add update the content of file
Here is the possibly dangerous issue. What exactly do you mean by "update"? Do you mean (a) replace certain bytes (b) overwrite whole file or (c) append to what is there? They all need different "open" code.
In your other thread you showed
QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt"); if (!file.open(QIODevice::ReadWrite)) { qDebug() << "file is not exist"; qDebug()<< "Cannot open file for writing: " << qPrintable(file.errorString()); } else { QTextStream out(&file); out << secondsbetweenDateTime<< endl; }
When the file does not already exist then (I believe)
ReadWrite
creates it anew at zero length. This path is OK. However, what about when it does exist, yourelse
code?ReadWrite
does not truncate the existing content and it does not append to it; rather, it just starts writing new bytes from the start "on top" of whatever happens to already be in the file. So say your existing file currently containsHello world
Your code opens for read-write, say you write
Bye
to that file, and then you're done and close it. the file will now contain:Byelo world
Presumably not what you intended! This is why
ReadWrite
is rarely what people actually mean, certainly for a text file.So if you want further help indicate what you really want to do with this file.
-
@Qt-embedded-developer said in how to create file if it not exist at give path in qt ?:
i have to add update the content of file
Here is the possibly dangerous issue. What exactly do you mean by "update"? Do you mean (a) replace certain bytes (b) overwrite whole file or (c) append to what is there? They all need different "open" code.
In your other thread you showed
QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt"); if (!file.open(QIODevice::ReadWrite)) { qDebug() << "file is not exist"; qDebug()<< "Cannot open file for writing: " << qPrintable(file.errorString()); } else { QTextStream out(&file); out << secondsbetweenDateTime<< endl; }
When the file does not already exist then (I believe)
ReadWrite
creates it anew at zero length. This path is OK. However, what about when it does exist, yourelse
code?ReadWrite
does not truncate the existing content and it does not append to it; rather, it just starts writing new bytes from the start "on top" of whatever happens to already be in the file. So say your existing file currently containsHello world
Your code opens for read-write, say you write
Bye
to that file, and then you're done and close it. the file will now contain:Byelo world
Presumably not what you intended! This is why
ReadWrite
is rarely what people actually mean, certainly for a text file.So if you want further help indicate what you really want to do with this file.
@JonB IN MY CASE YOUR a AND b OPTION POSSIBLE
I NEED TO DO LIKE BELOW:
FILE CONTENT PREVIOUS :
POWER ON TIME : 1000I HAVE TO READ ABOVE CONTENT AND CHANGE IT LIKE BELOW IF POWER ON TIME IS 12
FILE CONTENT NOW:
POWER ON TIME: 12 -
@JonB IN MY CASE YOUR a AND b OPTION POSSIBLE
I NEED TO DO LIKE BELOW:
FILE CONTENT PREVIOUS :
POWER ON TIME : 1000I HAVE TO READ ABOVE CONTENT AND CHANGE IT LIKE BELOW IF POWER ON TIME IS 12
FILE CONTENT NOW:
POWER ON TIME: 12CHANGE IT
And does that mean overwrite what is there or append to what is there? I would do one of:
-
If you do use
QIODevice::ReadWrite
and want to append I would gofile.open(QIODevice::ReadWrite | QIODevice::Append)
, to ensure code does not overwrite what is already there. -
If you want to overwrite after reading, then do not use
QIODevice::ReadWrite
. As I wrote above it will leave extra characters in the file if the new output is any shorter than the existing content. Instead do the next step.... -
Alternative is to open file
ReadOnly
, do reading, close. Then re-open for either overwrite/truncate or append and write the new stuff now.
In all of above cases, if you are under Windows you should or in
QIODeviceBase::Text
since this is a text file. -
-
CHANGE IT
And does that mean overwrite what is there or append to what is there? I would do one of:
-
If you do use
QIODevice::ReadWrite
and want to append I would gofile.open(QIODevice::ReadWrite | QIODevice::Append)
, to ensure code does not overwrite what is already there. -
If you want to overwrite after reading, then do not use
QIODevice::ReadWrite
. As I wrote above it will leave extra characters in the file if the new output is any shorter than the existing content. Instead do the next step.... -
Alternative is to open file
ReadOnly
, do reading, close. Then re-open for either overwrite/truncate or append and write the new stuff now.
In all of above cases, if you are under Windows you should or in
QIODeviceBase::Text
since this is a text file. -
-
@JonB said in how to create file if it not exist at give path in qt ?:
If you want to overwrite after reading, then do not use QIODevice::ReadWrite
You can, simply do resize(0) before writing
@mpergand
Indeed, which probably callstruncate()
. It does not say whether you must thenseek(0)
as well, many platforms require seek-when-change-from-read-to-write. It's yet another way. Personally I probably would not open forReadWrite
and do that. The OP now has a choice of approaches.