By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
bobrics wrote:
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

Nov 15 '05 #2

P: n/a
Thank you for the fast reply.

Nov 15 '05 #3

P: n/a
"bobrics" <bo*****@gmail.com> wrote:
# 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");

--
SM Ryan http://www.rawbw.com/~wyrmwif/
But I do believe in this.
Nov 15 '05 #4

P: n/a
thank you,

this is interesting to know as well

Nov 15 '05 #5

P: n/a
bobrics wrote:
thank you,

this is interesting to know as well


What is?
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #6

P: n/a
SM Ryan wrote:
"bobrics" <bo*****@gmail.com> wrote:
# 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 :-)

--
Simon.
Nov 15 '05 #7

P: n/a
On 2005-11-15, Simon Biber <ne**@ralmin.cc> wrote:
SM Ryan wrote:
"bobrics" <bo*****@gmail.com> wrote:
# 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)
Nov 15 '05 #8

P: n/a
On Tue, 15 Nov 2005 03:45:31 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.