0

I have a file and I want to make a backup file, with the same name as file but with "aux" at the end. How do I do this? I tried:

char *nameFile, *nameAux;
char *aux = aux;
nameAux = nameFile + aux;

Which didn't work.. nameFile is given to me by the user, and it's right because I can open/ceate the file with nameFile

gsamaras
  • 71,951
  • 46
  • 188
  • 305

2 Answers2

1

You can't just "add" C strings. effectively you've summed pointers. See How do I concatenate const/literal strings in C?

Karol T.
  • 543
  • 2
  • 13
0

There is no + operator for strings in C.

You need to use strcat(), like this:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char* filename = "myfile";
    char backup_filename[10];
    strcpy(backup_filename, filename);
    strcat(backup_filename, ".AUX");
    puts(backup_filename);
    return 0;
}

Output:

myfile.AUX

gsamaras
  • 71,951
  • 46
  • 188
  • 305