1

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

1
  • Your title is somewhat misleading. You're not looking for filenames based on variables but for strings basd on variables. Commented Apr 14, 2020 at 15:08

1 Answer 1

3

You're looking for snprintf():

char filename[128];
snprintf(filename, sizeof(filename), "GenericName_%.2f_%.2f_%.2f.txt", x, y, z);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.