I am trying to open a txt file for a program entering the name of the file as a command line argument. if I provide the path as a string it works, so the problem in my code is with the command line argument. I have the following:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char* argv[])
{
char* filename = "/path/";
char* name = argv[1];
printf("%s\n", filename);
printf("%s\n", name);
strcat(filename, name);
printf("%s\n", filename);
strcat(filename, ".txt");
printf("%s\n", filename);
return 0;
}
I run it like so:
./program filenamewithoutextension
I get the following output when I run it:
/path/
filenamewithoutextension
Segmentation fault (core dumped)
I don't understand what is happening.