Results 1 to 12 of 12

Thread: rename file name

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default rename file name

    I use Qfile to open and rename a file,but I can not rename it.
    Qt Code:
    1. QFile file("..\\..\\media\\terrain.cfg");
    2. file.open(QIODevice::WriteOnly|QIODevice::Text);
    3. ...
    4. file.rename("tmp.cfg");// it always return fasle.
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rename file name

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

  3. #3
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: rename file name

    Hi

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

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

  4. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rename file name

    there's no need to close file before renaming. From QFile::rename documentation
    The file is closed before it is renamed.

  5. #5
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rename file name

    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.

  6. #6
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: rename file name

    Have u closed that file before renaming ...

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

  7. #7
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: rename file name

    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.

  8. #8
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rename file name

    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?

  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rename file name

    Try using absolute paths for renaming...
    Also do you have rights to change file names on your system ?

  10. #10
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rename file name

    Quote Originally Posted by vishwajeet.dusane View Post
    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

    weixj2003ld, try this:
    Qt Code:
    1. QFile f( "a.txt" );
    2. if ( f.open( QIODevice::ReadOnly ) )
    3. {
    4. QString fileContent = f.readAll();
    5. // modify fileContent
    6. if ( f.open( QIODevice::WriteOnly | QIODevice::Trancate ) )
    7. {
    8. f.write( fileContent.toAscii() );
    9. f.close();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rename file name

    Quote Originally Posted by weixj2003ld View Post
    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.

  12. #12
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rename file name

    I have try the method borisbn provided,it is ok. thk u all.

Similar Threads

  1. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  2. QFile::rename()
    By Bagstone in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2008, 10:53
  3. How I add rename in QTreeWidgetItem?
    By rajesh in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2007, 12:10
  4. How to rename a file after opening ?
    By npc in forum Newbie
    Replies: 2
    Last Post: 1st June 2006, 13:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.