Thread: multiple file handling

  1. #1
    Registered User
    Join Date
    Nov 2005

    multiple file handling

    I am trying to write code that will open and modify multiple files with similar prefixes and different "numbers," ie, data001, data002 etc. I have written something that I thought would work, but it seems to be faulty, please help.

    int main(void)
    	//these are the variables. You need to put your data files in the folder where this is compiled
    	//m->slope, x->velocity, y-> number of counts and sums. 
    	//n->number of points in background
    	double m, x, y;
    	double leftend, rightend; /*these define the region where there is absorption*/
    	int n=0, numpoints;
        FILE *in;  /* our file pointer */
                   /* the file name (we'll change the number later) */
    	FILE *newfold; /*the output file*/
    	char *fname = "data000.txt";
    	char *output = "folded000.fld";
                   /* the digits as characters */
        char digit[10] = {'0','1','2','3','4','5','6','7','8','9'};
        int i;     /* this is our counter */
        int d1;    /* this is the hundreds digit */
        int d2;    /* this is the tens digit */
        int d3;    /* this is the ones digit */
    	printf("At what velocity does the absorption begin? (mm/s): ");
    	scanf("%lf", &leftend);
    	printf("At what velocity does the absorption end? (mm/s): ");
    	scanf("%lf", &rightend);
        for( i = 1; i <= 100; i++ ){
           d1 = i/100;             /* form file name */
           d2 = (i - 100*d1)/10;
           d3 = (i - 100*d1 - 10*d2);
    	   printf("d1 = %d, d2 = %d, d3 = %d\n", d1, d2, d3);
    	   /*the program crashes after this step*/
    	   fname[4] = digit[d1];   /* assume less than 999 files */
    thanks in advance


  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    Well, your digit array has 10 elements.

    But what does i/100 give you when i is 0 through 100? The highest result you'll ever get is 1 and the lowest is 0.01. But since you're only working with integers the result will be 99 0s and then finally a 1 when i reaches 100.

    How about doing this instead?
    #include <stdio.h>
    int main(void)
      char filename[12];
      int ctr;
      for(ctr = 0;ctr < 1000;++ctr)
        sprintf(filename, "data%03d.txt", ctr);
        /* Rest of code to play with each file goes here */
      return 0;
    The %03d makes it so that you'll get a 3-digit number every time. So filename will be data000.txt, then data001.txt, all the way up to data999.txt.
    Last edited by itsme86; 11-18-2005 at 10:58 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Wow, thanks a lot itsme, that worked fine and is a lot more transparent than my method. Talk to you later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM