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

Discussion in 'C Programming' started by bobrics, Nov 14, 2005.

  1. bobrics

    bobrics Guest

    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
    bobrics, Nov 14, 2005
    #1
    1. Advertising

  2. 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
    Antonio Contreras, Nov 14, 2005
    #2
    1. Advertising

  3. bobrics

    bobrics Guest

    Thank you for the fast reply.
    bobrics, Nov 14, 2005
    #3
  4. bobrics

    SM Ryan Guest

    "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?

    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.
    SM Ryan, Nov 14, 2005
    #4
  5. bobrics

    bobrics Guest

    thank you,

    this is interesting to know as well
    bobrics, Nov 14, 2005
    #5
  6. bobrics

    Default User Guest

    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.
    Default User, Nov 15, 2005
    #6
  7. bobrics

    Simon Biber Guest

    SM Ryan wrote:
    > "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?
    >
    > 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.
    Simon Biber, Nov 15, 2005
    #7
  8. bobrics

    Jordan Abel Guest

    On 2005-11-15, Simon Biber <> wrote:
    > SM Ryan wrote:
    >> "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?
    >>
    >> 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)
    Jordan Abel, Nov 15, 2005
    #8
  9. On Tue, 15 Nov 2005 03:45:31 +0000 (UTC), in comp.lang.c , Jordan Abel
    <> 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 =----
    Mark McIntyre, Nov 15, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,002
    Travis Murray
    Aug 24, 2004
  2. Nonee
    Replies:
    2
    Views:
    2,634
    Neredbojias
    Oct 25, 2005
  3. Replies:
    1
    Views:
    1,431
    Roland de Ruiter
    Jun 15, 2006
  4. Ed
    Replies:
    10
    Views:
    45,727
    alok000707
    Jul 13, 2010
  5. Michel Rouzic
    Replies:
    4
    Views:
    1,820
    Michel Rouzic
    Apr 28, 2008
Loading...

Share This Page