[Solved] Reading from a *.txt file in a loop [closed]


I think something like this is what you are aiming for, which will work with C89, C99, and beyond:

int k;
int input[20][WIDTH][HEIGHT];  // where WIDTH and HEIGHT are 
                               // compile-time constants
...
for ( k=0; k<20; k++ )
{   
  char fname[10];
  sprintf(fname, "input_%d", k);
  FILE *fr = fopen(fname, "r");
  if (fr)
  {     
    int i;    
    for (i=0; i<WIDTH; i++ )
    {  
      int j;           
      for (j=0; j<HEIGHT; j++ )
      {                 
        fscanf( fr, "%d", &input[k][i][j] );             
      }           
    }
    fclose(fr);     
  } 
}

However, I’m making a number of assumptions about your intentions here, so this may not be what you really want.

EDIT

Fixed some typos, got rid of width and height variables.

2

solved Reading from a *.txt file in a loop [closed]