How can I rename a file in c++?
rename(tempFileName.c_str(), tempFileName.c_str()+"new.txt");
But tempFileName
is of type std::wstring
. But rename()
functions accepts only const char*
parameters.
In Visual C++, the wide-character version of rename() is _wrename(). This isn't portable, but you may not care about that. Also, you can't add raw string pointers like that, you want something like this (not tested):
std::wstring newName(tempFileName);
newName += L"new.txt";
_wrename(tempFileName.c_str(), newName.c_str());
When working with Visual Studio
, you usually work with wide-strings. In order to rename the file you can use MoveFileEx
-function, you can rename the file like this.
std::wstring newFilename = tempFileName.c_str();
newFilename += _T("new.txt");
if(!MoveFileEx(tempFileName.c_str(), newFilename.c_str(), flags )){
//error handling if call fails
}
See here for the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With