PDA

View Full Version : rename file name



weixj2003ld
5th April 2010, 05:53
I use Qfile to open and rename a file,but I can not rename it.

QFile file("..\\..\\media\\terrain.cfg");
file.open(QIODevice::WriteOnly|QIODevice::Text);
...
file.rename("tmp.cfg");// it always return fasle.

borisbn
5th April 2010, 07:28
from Qfile::rename documentation


If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).

vishwajeet.dusane
5th April 2010, 09:05
Hi

Use static implementation of rename and when u do it close open file.


QFile::rename("..\\..\\media\\terrain.cfg","tmp.cfg")

borisbn
5th April 2010, 09:10
there's no need to close file before renaming. From QFile::rename documentation

The file is closed before it is renamed.

weixj2003ld
5th April 2010, 11:46
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?

vishwajeet.dusane
5th April 2010, 11:47
Have u closed that file before renaming ...

Could u paste that code snipped now after changing the code ?

vishwajeet.dusane
5th April 2010, 11:54
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 statement
The file is closed before it is renamed.

Does that mean implicitly this will be done or developer has to that explicitly.

weixj2003ld
5th April 2010, 11:59
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?

aamer4yu
5th April 2010, 12:17
Try using absolute paths for renaming...
Also do you have rights to change file names on your system ?

borisbn
5th April 2010, 20:47
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 statement

Does that mean implicitly this will be done or developer has to that explicitly.

yea, it means, that you don't have to close files see QFile::rename docs (http://qt.nokia.com/doc/4.6/qfile.html#rename)

weixj2003ld, try this:


QFile f( "a.txt" );
if ( f.open( QIODevice::ReadOnly ) )
{
QString fileContent = f.readAll();
// modify fileContent
if ( f.open( QIODevice::WriteOnly | QIODevice::Trancate ) )
{
f.write( fileContent.toAscii() );
f.close();
}
}

kevinchannon
6th April 2010, 15:18
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.?

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.

weixj2003ld
8th April 2010, 11:27
I have try the method borisbn provided,it is ok. thk u all.