0

Hey,

May someone please explain to me why the rename function isn't working? result should display a 0 if the files were sucessfully renamed. it keeps returning a -1.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
using namespace std;

int main(int argc, char *argv[])
{
   bool Stats;
   char *temp;
   int result;
   ifstream in1;
   ofstream out1, out2;
   
   in1.open(argv[1], ios::binary);					 
   if (!in1)
   {
	   cout << "That is not a valid file.  Try again!\n";
   }
   else
   {
	  temp = new char [strlen(argv[1]) + 5];
	  strcpy(temp, argv[1]);
	  strcat(temp, ".txt");
	  out1.open(temp, ios::binary);
	  
	  Stats = FixFile(out1, in1); 

	   remove(argv[1]);
	   result = rename(temp, argv[1]); 
	   cout << result;
	  }
   return 0;
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ana_1234
1

May someone please explain to me why the rename function isn't working?

rename() sets errno. Call perror() to get a more useful error message.

Votes + Comments
Yes
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.