Originally Posted by
awb8.
I have been told not to use an array, so I have been trying to avoid an array and would also like to keep the file short.
I have 20 files with a random number of values in each, and need to do the same calculations to each file and print them at the end of the code. I've been trying to use a for loop to generate the file number e.g.
Code:
for (z=10; z==20; z++)
{
infile = fopen(("data-%d.txt", z) "r");
}
if (infile == NULL)
{
printf("Can't open data-%d.txt\n");
system("pause");
exit(1);
}
Ok that can work... but you need an extra step in there... fopen doesn't understand text formatting....
Code:
char fName[16];
for (z=10; z<21; z++) // assuming numbers from 10 to 20 inclusive
{
sprintf(fName,"data-%d.txt",z);
infile = fopen(fName "r");
if (infile == NULL)
{
printf("Can't open %s \n",fName);
system("pause");
exit(1);
}
// process your data here
} // end of for loop
In the interests of clarity you may want to make your file processing into a function call and call it from the spot marked but there's nothing wrong with doing it all in one big loop.