0
I have a question about how to divide information from a text file into variables in C. Example, I have a txt file with the following data:
3 2
A
D
E
AD
DE
I would like to read this file, and separate each information into a variable, for example, the number 3 in one variable, the 2 in another, the A in another and so on, the letters together can be in one variable only.
So far what I’ve achieved in terms of code is this:
void ler_arquivo(){
int vertices;
int arestas;
FILE *arq = fopen("init.txt","r");
if(arq != NULL){
printf("\tSucesso!\n");
char linha[3];
while(!feof(arq)){
fgets(linha,3,arq);
printf("%s",linha);
}
}
}
For now I’m just printing the vector, because I don’t know how to do the division into variables.
Search for the Strtok function of <string. h>.
– anonimo
cppreference.com - Strtok
– Leonardo Alves Machado