Hi everyone,
Im trying to write a piece of code which, when the filename is typed by the user, reads in a number of files of the same name with one differing character (a number on the end). Ive started a bit of code now and its not finished or working at all really but I was just wondering if anyone had any tips as what to do next with it because im kinda stuck! Each of the files needs to be read into an array. So for example if I have 5 files that I want read they need to be stored in 5 different arrays. I know the read function im using justnow wont do that. Any suggestions for how I should modify the code?
Code:#include <stdio.h>
#include <stdlib.h>
void read(char[]);
int main(){
FILE *fileHandle;
int i, j, iNumFiles;
char sFileName[100];
printf("Please enter the number of files you wish to use: ");
scanf("%d", iNumFiles);
printf("Please type the name of the file you wish to use: ");
scanf("%s", sFileName);
for(i=0;i<iNumFiles;i++)
{
sprintf(sFileName, "%s_%i.dat", i);
read(sFileName);
}
}
void read(char sFileName[]){
FILE* fileHandle;
int array[64][64];
int loopCounterX = 0;
int loopCounterY = 0;
if((fileHandle = fopen(sFileName, "r")) == NULL)
{
printf("Unable to open file : %s\n", sFileName);
}
else
{
for(loopCounterX = 0; loopCounterX < 64; loopCounterX++)
{
for(loopCounterY = 0; loopCounterY < 64; loopCounterY++)
{
fscanf(fileHandle, "%d", &array[loopCounterX][loopCounterY]);
}
}
fclose(fileHandle);
}
}