Thread: Generating data file names from program variables

  1. #1
    Registered User a380x's Avatar
    Join Date
    Dec 2006

    Question Generating data file names from program variables

    Hi Everybody,

    I am writing a C program for numerical scheme. Now, based on the input parameter, I am trying to generate data file name. For example, suppose grid size defined by N is 50 i.e. N = 50, and if I can include this grid size in the data file e.g. ums_50.dat, then as we change the parameters, they will automatically be reflected in the file name. Let's say we make N = 60, then code should generate file ums_60.dat and so on.

    We can also have more than one parameters in the file name. I did write a code which is a bit involved and not giving desired output.

    Any relevant suggestions will be appreciated.

    Thanx in advance.


  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    show your code

    use sprintf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User a380x's Avatar
    Join Date
    Dec 2006

    Thumbs up

    Hi Vart,
    That was really simple. Getting the required results.
    Thank you so much.
    Here's a snippet I quickly wrote using sprintf().
        int     N,i,j;
        char    string[50];
        double  lambda;
        N       = 100;
        i       = 10;
        j       = 20;
        lambda  = 0.01;
        sprintf( string, "i=%d_j=%d_N=%d_lambda=%0.3f.dat", i,j,N,lambda);
        fp1 = fopen( string, "w" );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM