| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Unable to rename files in C++
When I use rename(oldfile, newfile) ....I am NOT getting erro. The file is renamed
But the renamed file is a ZERO byte file. How do I fix this? I AM NOT GETTING ANY ERROR MESSAGES .... .... if ( rename( exportFileName, new_export_file_name ) != 0 ) { cout << "Unable to rename " << exportFileName << " to " << new_export_file_name << " Terminating ... " << NEW_LINE; db2Helper.disconnectFromDB();exit(1); } .... .... |
|
#2
|
|||
|
|||
|
Quote:
I had the same problem that you do, all of my renamed files were 0 bytes in size for some reason. I finally figured it out. This code should fix your problem. I wish that they had more useful documentation on the rename() function in ANSI C++. Don't forget to include <iostream> and <fstream> Code:
// ios::in opens in read only mode, ios::nocreate do not create the file if it doesn't exist.
ofstream inFile;
inFile.open("MyFile.txt", ios::in | ios::nocreate);
// Check to make sure the file exists before proceeding
if (!inFile) {
// Close the phantom file, exit with error
inFile.close();
cout << "Error no file." << endl;
return -1;
}
else {
// First close file so it's not in use!
inFile.close();
// Everything is ok, so rename the file
int result;
result = rename("MyFile.txt", "NewFile.txt";
// Error check
if (result != 0 ) {
cout << "Error on rename." << endl;
return -1;
}
// Got to here, so successful rename
cout << "successful rename" << endl;
return;
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Unable to rename files in C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|