Jump to content
  • Advertisement
Sign in to follow this  
Infinity95

Moving file doesn't work

This topic is 2012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i'm having trouble finding the Problem. It never moves the file to it's new destination. Maybe im just too tired but please give me some ideas why it's not working.

Here's the relevant code:

 

 

void Logger::RotateLogFiles() {
    ostringstream newpath;
    string oldpath;
    this->logtime = time(0);
    this->now = localtime(&this->logtime);

    newpath << this->archive << "/" << this->now->tm_mday << "-" << this->now->tm_mon + 1 << "-" << this->now->tm_year + 1900 << "/" << this->filepath.substr(this->filepath.find(".")+1, string::npos);
    if(rename(this->filepath.c_str(), newpath.str().c_str()) != 0) MessageBox(NULL, L"Error moving Logfile.", L"Error", MB_OK | MB_ICONERROR);
}
 

 

Share this post


Link to post
Share on other sites
Advertisement

It looks like your "newpath" contains some directories. Do these directories exist?  Does the code work if you manually create these directories?

Share this post


Link to post
Share on other sites

Ill try that tomorrow. Going to bed now cause it's kinda late here in germany. Thanks in advance for helping and maybe i'll even solve it myself tomorrow after a good night's rest ;)

Share this post


Link to post
Share on other sites

If the paths are different this may not work depending on the OS you're running.

 

AFAIK Windows only allows rename if the drive is the same.

Edited by Endurion

Share this post


Link to post
Share on other sites

This strange behavior can be caused by excessive count of '/' symbols in `newpath` value.

Share this post


Link to post
Share on other sites

OK i just came home from school and im gonna try and figure out whats wrong after lunch.

@Endurion: I'm writing to the same drive so i don't think there is a problem with that.

@GuardianX: There's only two '/' in 'newpath' so that shouldn't be a problem either.

 

EDIT:

Here's some debug values:

newpath is: "/Logs/5-6-2013/14_16_44-Default.log"

filepath is: "5-6-2013.Default.log"

 

I dont see what i did wrong. The folders in newpath all exist.

 

EDIT2: Ok, seems like i cant use rename to move a file into a folder... is there any other function to do that, thats easy to use?

 

EDIT3: I now tried using MoveFile from the windows library and it still doesn't seem to work. Do i  have to use an absolute path maybe instead of a relative one?

if(!MoveFile((TCHAR*)this->filepath.c_str(), (TCHAR*)newpath.str().c_str())) MessageBox(NULL, L"Error moving Logfile.", L"Error", MB_OK | MB_ICONERROR);
Edited by Infinity95

Share this post


Link to post
Share on other sites

Any help on this? I still haven't found a solution to this and im kinda stuck on what i should do next to find a solution.

 

*Bump*

Share this post


Link to post
Share on other sites

OK i got the error message now. It says it can't find the file. The file is definitely closed before moving it and it definitely exists. I checked the variables value in the debugger.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!