Maybe I want to write integers from 1 to 10 in the first row with a loop.
THen in another loop I want to add 10 random numbers with
rand() in the second row.
Maybe I'll just write some numbers in a file to make the example simple.
Then I want to only read the integers from the first row and put them in an array.
Or I only want to read the Integers from the second row and put them in an array.
Here's one way.
Code:
#include <stdio.h>
int gimme(const char *filename, int row, int *array, size_t size)
{
FILE *file = fopen(filename, "r");
if ( file )
{
size_t i;
while ( row-- )
{
for ( i = 0; i < size; ++i )
{
fscanf(file, "%d", &array[i]);
}
}
for ( i = 0; i < size; ++i )
{
printf("%d,", array[i]);
}
putchar('\n');
fclose(file);
}
}
int main(void)
{
int array[5];
gimme("file.txt", 1, array, sizeof(array)/sizeof(*array));
gimme("file.txt", 3, array, sizeof(array)/sizeof(*array));
gimme("file.txt", 2, array, sizeof(array)/sizeof(*array));
return 0;
}
/* file.txt
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
*/
/* my output
1,2,3,4,5,
11,12,13,14,15,
6,7,8,9,10,
*/