I need to read txt files in C and interpret the data from within the files. The files contain parts in TEXT and others in DECIMAL / WHOLE numbers. What I need is to read the whole file and pass the numbers to a Cartesian coordinate structure. For example, the contents of the file are:
NAME : eil8
COMMENT : 8-city problem (Christofides/Eilon)
TYPE : TSP
DIMENSION : 8
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 37 52
2 49 49
3 52 64
4 20 26
5 40 30
6 21 47
7 17 63
8 31 62
EOF
I need to read and allocate data from
1 37 52
2 49 49
3 52 64
4 20 26
5 40 30
6 21 47
7 17 63
8 31 62
EOF
in a structure so it looks more or less like this:
coordenada.x[1] = 37;
coordenada.y[1] = 52;
coordenada.x[2] = 49;
coordenada.y[2] = 49;
...
But I do not know enough about dynamic allocation. What I thought:
1- abrir o arquivo
2- ler o arquivo linha por linha até encontrar "NODE_COORD_SECTION"
3- ler o arquivo linha por linha
4- alocar dinamicamente os dados em uma estrutura tipo matriz x * y
5- parar em EOF
I have knowledge of the code up to the reading portion of the file, but since each file will have a different number of lines, how do I read it without a pre-set size?