d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Trying To Rename A File With C++ In Microsoft Stud
Add Reply New Topic New Poll
Member
Posts: 445
Joined: Oct 26 2021
Gold: 2,676.12
Jun 1 2022 11:53am
I'm getting an error that says "c6031: return value ignored: 'rename'." I've added the following code to see whats going on after attempting the rename.
if (rename("temp.txt", "employeeRoster.txt") == -1)
{
std::string stop;
std::cout << "Error: " << strerror(errno) << std::endl;
getline(std::cin, stop);
}

The error this if statement gives me is "Error: File exists."
but prior to this if statement I have a line of code as follows
remove("employeeRoster.txt"); //deletes the old file employeeRoster.txt

so this should delete that old file before the rename occurs with temp.txt.
Anyone have any thoughts on this? I'm going to comment a large section of the code after I submit this post.
Member
Posts: 445
Joined: Oct 26 2021
Gold: 2,676.12
Jun 1 2022 11:54am
overTimeFile.close();
overTimeTemp.close();
overTimeTemp.open("overTimeTemp.txt");
temp.open("temp.txt");
while (getline(overTimeTemp, name))//while getting names from overTimeTemp
{
temp << name << std::endl;//store names from overTimeTemp.txt into temp.txt file
}
remove("overTimeTemp.txt"); //deletes overTimeTemp file
remove("employeeRoster.txt"); //deletes the old file employeeRoster.txt
rename("temp.txt", "employeeRoster.txt"); //renames the temp file to employeeRoster.txt ***an error is occuring here?***
temp.close();
if (rename("temp.txt", "employeeRoster.txt") == -1)
{
std::string stop;
std::cout << "Error: " << strerror(errno) << std::endl;
getline(std::cin, stop);
}
Member
Posts: 445
Joined: Oct 26 2021
Gold: 2,676.12
Jun 4 2022 01:51pm
I figured out the problem, in order to remove a file you have to close it, in order to rename a file you have to close and remove the file your trying to rename it too, as well as close the temp file.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll