renaming file with posix c

-1

I have a file called TempFile.txt I made the following script in c to rename file receiving the name of the file to be renamed and the new name, but it is returning this error:

Error renaming file: Bad address         Output new name:

The script is this below:

#include <stdio.h>

int main()
{
 char *oldname;
 char *newname;

 printf("\n\tInput name file: ");
 scanf("%s", oldname);

 printf("\n\tOutput new name: ");
 scanf("%s", newname);

 if(rename(oldname, newname) == 0)
  puts("File successfully renamed");
 else
  perror("Error renaming file");
 return 0;
}
    
asked by anonymous 07.03.2018 / 22:42

1 answer

1

Your error is because of input variables.

char *oldname;
char *newname;

They are pointers and do not have a size allocated to them, to repair their code declare the variables oldname and newname as vectors.

char oldname[64];
char newname[64];
    
07.03.2018 / 22:52