Thread: increment output filename within loop

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    12

    increment output filename within loop

    i have a loop that looks something like this:

    Code:
    fout1 = open_file_write("/path/to/output/file/output.dat");
    
    for (i=0; j<10; i++) {
    		for (j=0; j<N_bs; j++) {
    			
    			int r = rand() % data_list_d.size +1 ;			
    			//assign sub arrays for x,y,z with radnomly removed points
    			xsub_d[j]=data_list_d.data[r].x;
    			ysub_d[j]=data_list_d.data[r].y;
    			zsub_d[j]=data_list_d.data[r].z;
    		
    			fprintf(fout1,"%d %d %Le %Le %Le\n",j,r,xsub_d[j],ysub_d[j],zsub_d[j]);
    		}
    			function1(data_list_rad,data_list_r,xsub_d,ysub_d,zsub_d,N_bs); 
    			function2(data_list_rad,xsub_d,ysub_d,zsub_d,N_bs); 
    	}

    i wish to for every 'i' in that loop write to fout1 such that the output file is e.g. output'i'.dat
    that is: output1.dat, output2.dat..... and so on out to i<some limit in that loop.

    How do i do that?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    Quote Originally Posted by MWAAAHAAA View Post
    Thanks for nothing.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by renderg View Post
    Thanks for nothing.
    Dear Ungrateful,

    sprintf is the function you need to use.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Set the open_write_file() call just before the fprintf() call like in:

    Code:
    sprintf(buffer, "/path/to/output/file/output%d.dat", i);
    fout1 = open_file_write(buffer);
    fprintf(fout1,"%d %d %Le %Le %Le\n",j,r,xsub_d[j],ysub_d[j],zsub_d[j]);
    buffer must be a char * of enough size to hold the path (would be 31+1 null bytes plus the number of digits of the maximum i value). Well, you'll probably want to move the sprintf and open_file_write code to the very beginning of the outter loop, and add code to close the file at the end of the outter loop... The above was just pseudo-code.
    Last edited by roirodriguez; 08-27-2010 at 08:47 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've discovered one constant in the world of computers:

    For computer folk, Linux writers have an unholy alliance with the devil known as verbosity. What Borland fits into one tenth as many words, and makes clear, they can only make your eyes begin to glaze over.

    NEVER let a Linux programmer near a word processor!!


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
      char filename[20]="number";
      char ext[]=".txt";
      int i;
    
      printf("\n\n");
      for(i=0;i<101;i++) {
        sprintf(filename+6, "%d", i);
        strcat(filename, ext);
        printf("\n%s", filename);
        filename[6]='\0';
        if(i % 20 == 19)
          getchar();
      }
      printf("\n\n\t\t\t    press enter when ready");
      i=getchar();
      return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I find Adak approach ridiculous.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you a Linux programmer, Bayint?



    Haven't you heard the old saying?

    "Nature is harsh, life is short, and people are ridiculous."

    Proof of that concept can be seen in my Federal Gov't, who believes we can spend our way out of our national debt problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinite loop output problem
    By Jonyb222 in forum C Programming
    Replies: 5
    Last Post: 10-08-2009, 11:18 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM