Code:
char* strcat( char* dest, const char* src)
The way the strcat function works is that it copies the string src to the end of the string dest. That is the key, it does this by first walking along the destination string until it finds a null (the end point of the destination string) and then copying the character from the source string to that point. When you call strcat the first time, strcat(final , a), final is uninitialized and the function tries to find the end of the string which is supposed to exist there. Eventually it will find a null to stop at after who knows how many bytes it has read through and then start copying the string in a. Since this location where it is copying data to is unknown, you are probably overwriting something important and your program crashes. A solution to this is to initialize first by perhaps using strcpy instead of strcat. Also, as mentioned, txt is uninitialized as well so the second call to strcat is unadvised.
I think perhaps you meant to do this:
Code:
#include <stdio.h>
int main()
{
char a[20];
char final[50];
FILE * file;
printf("please enter file name: ");
scanf("%s",a);
strcpy(final , a);
strcat(final,".txt");
file = fopen(final,"w");
fprintf(file,"%s",a);
fclose(file);
return 0;
}