renaming file with Posix c

Asked

Viewed 26 times

-2

I have a file called Tempfile.txt I made the following script in c to rename file receiving the file name 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;
}
  • Whenever you publish a question or answer format the code with Ctrl-k or use the button {} of the editor

1 answer

1


Your error is because of the 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];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.