Variable FILENAME in fopen(). How to make it?

B

bobrics

Hi,

I would like to save data from multiple repetitive simulations into a
set of files. Each time a simulation restarts, I would like it to
automatically generate a file with a name containing some of the useful
variable information.
For example, if M is 5 and N is 10, I want the file to be named as
"myfile-5x10.txt". How can I do this?

I guess I need to construct a string somehow and let it be my filename.


Thank you
 
A

Antonio Contreras

bobrics said:
Hi,

I would like to save data from multiple repetitive simulations into a
set of files. Each time a simulation restarts, I would like it to
automatically generate a file with a name containing some of the useful
variable information.
For example, if M is 5 and N is 10, I want the file to be named as
"myfile-5x10.txt". How can I do this?

I guess I need to construct a string somehow and let it be my filename.

Declare a character array to contain your file name. The array should
be big enough to contain the longest filename you would generate.

Then google for sprintf and read the documentation.

HTH
 
S

SM Ryan

# Hi,
#
# I would like to save data from multiple repetitive simulations into a
# set of files. Each time a simulation restarts, I would like it to
# automatically generate a file with a name containing some of the useful
# variable information.
# For example, if M is 5 and N is 10, I want the file to be named as
# "myfile-5x10.txt". How can I do this?

char format[] = "myfile-%dx%d.txt";
char filename[sizeof format+100];
sprintf(filename,format,M,N);
FILE *file = fopen(filename,"w");
 
S

Simon Biber

SM said:
# Hi,
#
# I would like to save data from multiple repetitive simulations into a
# set of files. Each time a simulation restarts, I would like it to
# automatically generate a file with a name containing some of the useful
# variable information.
# For example, if M is 5 and N is 10, I want the file to be named as
# "myfile-5x10.txt". How can I do this?

char format[] = "myfile-%dx%d.txt";

Why make this a char[] instead of a const char *?
char filename[sizeof format+100];

Oh, it's so you can use sizeof? You could instead use a #define of
format as a string literal, then sizeof would work. I doubt there would
be any compilers that are brain-dead enough to then put two copies of
the literal into the executable, instead of making them share one memory
space!
sprintf(filename,format,M,N);
FILE *file = fopen(filename,"w");

Or, considering that you are using a C99 feature already (a declaration
after an executable statement), you could use strlen() to define a VLA :)
 
J

Jordan Abel

SM said:
# Hi,
#
# I would like to save data from multiple repetitive simulations into a
# set of files. Each time a simulation restarts, I would like it to
# automatically generate a file with a name containing some of the useful
# variable information.
# For example, if M is 5 and N is 10, I want the file to be named as
# "myfile-5x10.txt". How can I do this?

char format[] = "myfile-%dx%d.txt";

Why make this a char[] instead of a const char *?
char filename[sizeof format+100];

Oh, it's so you can use sizeof? You could instead use a #define of
format as a string literal, then sizeof would work. I doubt there would
be any compilers that are brain-dead enough to then put two copies of
the literal into the executable, instead of making them share one memory
space!

The operand of sizeof wouldn't need to exist in memory anyway.

I believe it's legal to sizeof a "dereferenced" uninitialized pointer

e.g. as commonly recommended, x = malloc(sizeof *x)
 
M

Mark McIntyre

I believe it's legal to sizeof a "dereferenced" uninitialized pointer

e.g. as commonly recommended, x = malloc(sizeof *x)

This is determining the size of the type the pointer points to, it
never looks at the uninitialised pointer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top