I have a file containing the following values:
10 20 30
40 50 60
For each row, the values must be stored in a vector.
How do I get such values from the file, knowing that there is no exact quantity for each line?
I have a file containing the following values:
10 20 30
40 50 60
For each row, the values must be stored in a vector.
How do I get such values from the file, knowing that there is no exact quantity for each line?
I'll assume you can read each of the lines in your file, okay?
This code here takes each number from a line:
#include <stdio.h>
#include <string.h>
int main() {
char linha[] = "10 100 1000";
char *num;
int x;
num = strtok(linha, " \n\r");
while (num != NULL) {
sscanf(num, "%d", &x);
printf("%d\n", x);
num = strtok(NULL, " \n\r");
}
return 0;
}
What it does and tokenizar
the line, that is, it extracts the elements of the string linha
. These elements are the 'things' between spaces or the end of the line, so the numbers.