I considered this solution but it requires "asking" and test number. I tried this code but I do have a problem with array size. Or 5 files under /tmp, bearing the following names: J0, J1, J2, J3, J4, J5. Each file is written the following values:
18
12
The source code:
Code:
#include <stdlib.h>
#include <stdio.h>
#define SIZE 4
int main()
{
int j, cpt, number1, number2;
int obs;
int tab[10];
char filename[3];
FILE *file;
for(j = 0; j <= SIZE; j++)
{
sprintf(filename,"/tmp/J%d",j);
if ((file = fopen ( filename, "r" )) == NULL)
{
perror ("Impossible to open the file.");
}
else
{
while ( fscanf ( file, "%d %d", &number1, &number2 ) == 2)
tab[j] = number2;
printf("tab[%d] = %d\n", j, tab[j]);
fclose (file);
}
}
printf("Number from? ");
scanf("%d",&cpt);
obs = 0;
for(j = 0; j <= SIZE; j++)
{
printf("tab[%d] = %d\n", j, tab[j]);
if(tab[j] == cpt)
{
obs = obs + 1;
}
}
printf("Total : %d\n", obs);
return EXIT_SUCCESS;
}
The output is:
tab [0] = 12
tab [1] = 12
tab [2] = 12
tab [3] = 12
array [4] = 12
Number from? 12
tab [0] = 877,277,040
tab [1] = 0
tab [2] = 12
tab [3] = 12
array [4] = 12
Total: 3
Why is my picture change?
Bookmarks