Hello everyone! When I use the fscanf function to read data from a text file, I want to read only the first few characters in a row, but not all the subsequent characters, and then read the next row. For example, my data file is as follows: 23 456 # Number 78 981 # Number I use Fscanf ("% d \ n", & A, & B ); Fscanf ("% d \ n", & C, & D ); The objective is to make a = 23, B = 456, c = 78, D = 981. When reading, you want to ignore the "# Number" string after each row. But I failed. How can I control the write format? You do not want to define another string variable to read this string. Thank you !! ----------------------------------------------------------------------
Reference: Fscanf ("% d % * s", & A, & B );
Fscanf ("% d % * s", & C, & D );
Brother, I have tried this method earlier. It does not work if there are multiple strings separated by spaces. --------------------------------------------------------------------
Reference: You didn't say there are multiple strings.
Fscanf ("% d % * [^ \ n] % * C", & A, & B ); Fscanf ("% d % * [^ \ n] % * C", & C, & D );
Thank you! After testing, the above method is good. At the same time, it is found that % * C in the format string can be changed to \ n. In addition, if you want to read numeric variables rather than character variables from the next row, it is no problem to omit % * C or \ n. However, if the next character is a character variable that needs to be read from the next row, % * C or \ n cannot be omitted. ---------------------------------------------------------------------- |