Home > Back-end >  Some problems about fscanf formatted input
Some problems about fscanf formatted input

Time:03-01

Text content is: [ABC] | | [ABC] [30]
I want to putting ABC a1, a2 stock ABC, a3 store 30
The code below

 # include & lt; Stdio. H> 
Int main () {
Char a1 [10], a2 [10].
Int a3;
The FILE * fp.
Fp=fopen (" data1. TXT ", "r");
Fscanf (fp, "[% s] | | [% s] [% d]", a1, a2, & amp; A3);
Printf (" % s ", a1);
return 0;
}


Finally my a1 all text content in the namely [ABC] | | [ABC] [30], but my a2 and a3 inside is what also have no, is where I went wrong

CodePudding user response:

% s default scanning by whitespace (Spaces, newlines, tabs, etc.) after the end of the
Suggest using Negated scanset, see http://www.cplusplus.com/reference/cstdio/scanf/
The following substitute sscanf fscanf test
 # include & lt; Stdio. H> 
# define TESTSTR "[ABC] | | [ABC] [30]"
# define NEGFMT "% [^]]"
# define DECFMT "% d
"Int main () {
Char a1 [10], a2 [10].
Int a3;
Sscanf (TESTSTR, "[]" NEGFMT "| | [" NEGFMT"] [" DECFMT "] ", a1, a2, & amp; A3);
Printf (" a1=% s, a2=% s, a3=% d \ n ", a1, a2, a3);
return 0;
}


Run results see https://www.ideone.com/aRQZQe

CodePudding user response:

Very thank, problem solved, also let me see the problem, thank you.
  • Related