Below is your code made even more minimal to reproduce the issue
#include <cstdio>
#include <cstring>
void ReadAllLine(const char *szCont)
{
int rdl = 0; /* read length */
int len = 0; /* total length */
char szLine[512] = {};
len = strlen(szCont);
while(rdl < len)
{
sscanf(szCont + rdl, "%s", szLine);
rdl += strlen(szLine) + 1;
printf("%s\n", szLine);
}
}
int main()
{
const char *str ="\n\n#Tag02";
ReadAllLine(str);
return 0;
}
This prints
#Tag02
2
The reason is quite simple. sscanf
ignores whitespace characters until it finds non-whitespace characters to push in to the output string. Thus, when you call sscanf(szCont + 0, "%s", szLine);
for the first time, it skips the 2 \n
s occuring first and then moves on to pushing #Tag02
into szLine
. Now the value of rdl = 0 + 7
since strlen("#Tag02") = 6
. This means szCont + rdl
would now be pointing to 2
, which gets printed in the next iteration.
The same issue occurs in your case too; your input has 2 \n\n
ocurrings, once after ENTRY02
, then after ENTRY22
.