How can I format fscanf to read strings of different lengths, some with spaces in them?
8 views (last 30 days)
I'm trying to read comet names from a text file with fscanf and when I use %s, the read stops at the first space in the name, then considers the remaining string to be the next data field. When I use %c which is supposed to read spaces, and format %Nc using a value of N that's large enough for the largest name, the shorter names have part of the next data field (large integers) read in as part of the name. BTW, not all of the names have spaces, so '%s%s' doesn't work either. Thank you.
dpb on 11 Oct 2017
Can't always read a mal-formed input file; if the data strings contain embedded blanks they need to be quoted strings.
Probably the only way to do this otherwise would be to read in each full record as a string then do a lookup to a known database--but if you make the database, there's probably no reason to read the file.