Thread: Printing To Multiple Files

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Printing To Multiple Files

    Hello, I am new to programming and Physics grad student(be easy on me ;-)).

    I have a program which saves data to an outfile in the folder sigma0.lambda0

    i.e. /home/user/Foo/sigma0.lambda0/

    However, I want to change the parameters sigma and lambda which I obtained by reading in a parameter file.

    Sigma and Lambda are ints, so they can be simply changed with a for loop.

    However, after changing these parameters and running the program again, I want to save the file into another folder

    /home/user/Foo/sigma1.lambda0/ etc....

    I have tried manually going into the char OUTFILE[101] and looping with a pointer until I find the the character of interest in OUTFILE and changing this manually. But it is cumbersome and I feel like someone will have a better solution.

    Edit: I already have the outfile directories made using a simple bash script.

    Thanks.
    Last edited by hasush; 04-18-2012 at 02:52 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look into sprintf:
    Code:
    sprintf(outfile, "/home/user/Foo/sigma%d.lambda%d...", sigma_val, lambda_val);
    Where sigma_val and lambda_val are ints. Also, all-uppercase identifiers are usually only used for constants and #defines, so I would change it to Outfile or outfile.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You may be looking for sprintf.
    Code:
    char OUTFILE[101];
    int i = 5, j = 7;
    sprintf(OUTFILE, "/home/user/Foo/sigma%d.lambda%d", i, j);
    Set i and j however you wish.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  4. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM