fopen messing up file name and changing the file name

76 Views Asked by At

This is my code (with debug prints):

char *full_file_name = malloc(sizeof(char));
FILE *ob_file;

/*creating full file name*/
strcpy(full_file_name, file_name);
strcat(full_file_name, ".ob");
printf("\n full_file_name :%s", full_file_name);

/* -- Creating the new file and inputting data --*/
ob_file = fopen(full_file_name, "w");
printf("\n full_file_name :%s", full_file_name);

And this is the print result: enter image description here

Why is this happening? What to do?

1

There are 1 best solutions below

0
NicknEma On

You’re only allocating memory for a single char. (You should check for NULL after malloc, too, btw)