SamuraiMelon
SamuraiMelon

Reputation: 367

Strings Based on Variables

Say I have variables double x, y, z; and I want to make a txt file with a name based on those variables e.g. "GenericName_x_y_z.txt".

How would I create the string?

I know functions like printf("GenericName_%.2f_%.2f_%.2f.txt", x, y, z) you can do, but how would I define a string like that, not just print it?

So then I can use

char filename[] = "GenericName_%.2f_%.2f_%.2f.txt";

FILE* fPointer;
fPointer = fopen(filename, "w");

I'm sorry the phrasing is really terrible and its probably a really basic thing I'm not getting!

Thanks

Upvotes: 1

Views: 41

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753615

You're looking for snprintf():

char filename[128];
snprintf(filename, sizeof(filename), "GenericName_%.2f_%.2f_%.2f.txt", x, y, z);

Upvotes: 3

Related Questions