0

I am looking to add multiple parts to a parameter.

Like this, I know know in VB.net i can do

messagebox("hello" + textbox1.text) // yields hello and whatever is in textbox1

but how would I do this in C?

I tried & and + but it give me errors.

i want to create 5 files, and use a for loop to create them, counting 1-5

ofp = fopen("data" + (loop counter) + ".txt", "r"); // allong those lines...

so the file name would be data1.txt;data2.txt;data3.txt so forth.

thanks

Alexej Magura
  • 4,833
  • 3
  • 26
  • 40
user3026473
  • 23
  • 1
  • 6

1 Answers1

0

In this case, you may use snprintf:

char buffer[FILENAME_MAX];
snprintf(buffer,FILENAME_MAX,"data%d.txt",counter+1); //You will probably start counter at 0

buffer[FILENAME_MAX-1]=0;// snprintf may skip null terminator.
FILE* ofp=fopen(buffer,"w");
//...
fclose(ofp);
user877329
  • 6,717
  • 8
  • 46
  • 88
  • 2
    The second argument to `snprintf()` could be `FILENAME_MAX` (or even better, `sizeof buffer`). –  Dec 26 '13 at 19:00