Don't forget that fgets()
would read in the trailing newline (\n
) as well. You may want to remove that.
fgets(filename,50,stdin);
filename[strlen(filename)-1]='\0';
After that make the new file names.
char names[3][60];
sprintf(names[0], "1TEMP%s", filename);
sprintf(names[1], "1FLUX%s", filename);
sprintf(names[2], "1PRESSURE%s", filename);
Then use these strings as argument to fopen()
like
fopen(names[0], "w");
And check if fopen()
succeeded by having a look at its return value. If it's NULL
, some error has occurred.
As for fp = fopen('1FINALTEMP'filename,"w");
giving error, '1FINALTEMP'
is a string and not a character literal. Single quotes are for character literals. See here.
And only adjacent string literals are concatenated as mentioned here.
Adjacent string literal tokens are concatenated.
ie, in printf("%s", "hello " "world");
, the "hello " "world"
would become "hello world"
.
But that's only for string literals.
Edit: snprintf()
is preferred over sprintf()
as the latter may write out of bounds if the string to be written is too large to be stored in the string to which writing occurs. With snprintf()
, you can specify the number of bytes to be written.
char name[60];
snprintf(names, 60, "1TEMP%s", filename);