How to open, write data to and close a new file for each step of a loop.

This is a discussion on How to open, write data to and close a new file for each step of a loop. within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm having a problem with a program I'm writing. I'm doing a numerical simulation, and as part of ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    12

    How to open, write data to and close a new file for each step of a loop.

    Hi everyone,

    I'm having a problem with a program I'm writing. I'm doing a numerical simulation, and as part of the simulation, I need to output a data file for every timestep, or later output the data of ten timesteps to a data file etc.

    The problem is that I would like to be able to vary the filename in each iteration of the loop in a counter fashon.

    For example data 1.dat
    data 2.dat
    and so on.

    I was wondering if anyone knows a way that this can be accomplished.

    All help you can give is very welcome and will be greatly appreciated.

    Thanks,

    Wayne.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can generate filenames using
    Code:
    char fname[20];
    sprintf(fname, "file%03d.dat", num);
    The other bits are pretty basic file handling, so you either already know that, or you need to read up on file IO basics.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    Thanks you for your fast reply.

    The other bits are fine, this will literally be the last touch the code.

    Thanks again, very much appreciated.

    Wayne.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21