I use Qfile to open and rename a file,but I can not rename it.
Qt Code:
... file.rename("tmp.cfg");// it always return fasle.To copy to clipboard, switch view to plain text mode
I use Qfile to open and rename a file,but I can not rename it.
Qt Code:
... file.rename("tmp.cfg");// it always return fasle.To copy to clipboard, switch view to plain text mode
from Qfile::rename documentation
If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).
Hi
Use static implementation of rename and when u do it close open file.
QFile::rename("..\\..\\media\\terrain.cfg","tmp.cf g")
there's no need to close file before renaming. From QFile::rename documentation
The file is closed before it is renamed.
I have used static implementation of rename,problem still exists.
I want to modify some contents of a text file,and do as follows:
1.open the text file(a.txt),and read the contents of it and modify some of it.
2.write the modified contents in a new file(b.txt);
3.delete a.txt
4.rename b.txt to a.txt.
now rename can not work properly,what other ways should I go?
Last edited by weixj2003ld; 5th April 2010 at 10:58.
Have u closed that file before renaming ...
Could u paste that code snipped now after changing the code ?
I m sorry but which document u r referring (is it assistant). It does not say anywhere that no need to close file before renaming.
Please correct me if i m wrong.
However i m confused with statementDoes that mean implicitly this will be done or developer has to that explicitly.The file is closed before it is renamed.
Thk u for your answer.
I have used static implementation of rename,problem still exists.
I want to modify some contents of a text file,and do as follows:
1.open the text file(a.txt),and read the contents of it and modify some of it.
2.write the modified contents in a new file(b.txt);
3.delete a.txt
4.rename b.txt to a.txt.
now rename can not work properly,what other ways should I go?
Try using absolute paths for renaming...
Also do you have rights to change file names on your system ?
yea, it means, that you don't have to close files see QFile::rename docs
weixj2003ld, try this:
Do you have a specific reason for doing all these steps? If you:
1. open the file
2. read in the data
3. close the file
4. change the bits you want to change
5. re-open the file (but for writing this time)
6. write the the new data
this should write over all the old data. That way you don't have to worry about changing the name at all.
I have try the method borisbn provided,it is ok. thk u all.
Bookmarks