I have a text file (txt) that contains the following values:
12 90
These two values I keep and my variables a
and b
, that is a
is equal 12
and b
is equal 90
, and I'm using scanf()
function to receive these values in the following way:
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("Valor a = %d, b = %d", a, b);
return(0);
}
Output:
Value a = 12, b = 90
I run the program with the following command scanfTeste.exe < arquivo.txt
at the prompt of Windows to run the program.
However, the structure of my file will change, time it will have two values or an indeterminate amount, see the examples:
- Example one of the contents file:
12 90
- Example two of the contents file:
12 90 33 77
Considering that values are separated by spaces, to make it easier to read the data.
My question:
As you can see the amount of parameters passing to scanf("%d %d ...", &a, &b, ...)
changes depending on the amount of values in the file line, how can I make the scanf()
function receive a number of parameters according to the amount of values in the file line?