-3

I'm trying to create a number of files in which I define the first part of the filename, then use a user inputted string for the second part. so if user puts in "filename", I want to create 1TEMPfilename, 1FLUXfilename, 1PRESSUREfilename

so far:

main ()
{

  char filename[64];

  printf("Enter the Runfile ID (e.g. jul0476.a\n");

  fgets(filename,50,stdin);

  //create new files

  FILE *fp;

  fp = fopen('1FINALTEMP'filename,"w");

}

this generates an error, any ideas on how to do this?

  • Please show the error message that you receive. – mkrieger1 Oct 16 '17 at 15:21
  • 2
    you question is - how do i concatenate strings in c. Use snprintf – pm100 Oct 16 '17 at 15:21
  • error is: warning: multi-character character constant [-Wmultichar] fp = fopen('1FINALTEMP'filename,"w"); ^ warning: character constant too long for its type error: expected ')' fp = fopen('1FINALTEMP'filename,"w"); ^ note: to match this '(' fp = fopen('1FINALTEMP'filename,"w"); – clairegetz Oct 16 '17 at 15:23
  • use `strcat(str1,filename)` where str1 is `"1FINALTEMP"` before fopen – krpra Oct 16 '17 at 15:27
  • if I concatenate the strings won't it change the filename string I'm trying to use? So I'd end up with 1TEMPfilename and 1FLUX1TEMPfilename.... ect? – clairegetz Oct 16 '17 at 15:27
  • using strcat i get an Abort trap 6 error – clairegetz Oct 16 '17 at 15:32
  • Create a 2nd buffer: `char full_name[64]; snprintf(full_name, 64, "1TEMP%s", filename); fp = fopen(full_name, "w");` – 001 Oct 16 '17 at 15:35
  • cool that worked! except all my file names end in "?" but I'll sort that out later! Thank you @JohnnyMopp – clairegetz Oct 16 '17 at 15:45
  • More likely, they all end with newline (which you typed when entering the name on stdin, but didn't ever remove). – Toby Speight Oct 16 '17 at 16:44
  • BTW, you ought to `#include ` and turn your compiler warnings up a bit - relying on implicit function definitions can cause surprises. – Toby Speight Oct 16 '17 at 16:45

2 Answers2

0

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);
J...S
  • 5,079
  • 1
  • 20
  • 35
-1

Try taking a string variable which store filename from fgets and then use '+' and concatenate what you provide (1Finaltemp) then pass it to file pointer like fp= fopen( myNewFile,w)