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;

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.

commented: Yes +17

thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.