c - Number of rows of a file is not known a priori -
an exercise in c tell me read infos file txt in the number of rows not known priori. example have file this:
name surname tel name1 surname2 tel2
i tought use function fscanf()
in way
file *fp ... while(fscanf(fp, "%s%s%s\n", name, surname, tel) != eof) { //function }
is right way?
h2co3 right fscanf returns number of fields matched.
the real problem don't know how many rows in file , not know how large structure or array allocate data. 1 solution use std:vector can grow.
it surprising how code reads file twice. once count lines, allocate storage , again read data. can sane solution.
Comments
Post a Comment