The below test code demonstrates that sscanf fails to parse the string "" (ie. an empty string inside "") in line2 (fErr=1). Is this a bug? Or is there maybe a workaround format string to be used with sscanf ?
/* sscanf_bug_demo.cpp sscanf fails scanning the string "" Compile: g++ -g -Wall -Wextra -std=c++11 sscanf_bug_demo.cpp Run: ./a.out Input lines: joe "prj 1" "blah foo" jim "" "quantum leap" Output: line1: n=3 : fErr=0 : un="joe" prj="prj 1" desc="blah foo" line2: n=1 : fErr=1 : un="jim" prj="" desc="" */ #include <cstdio> // using namespace std; void sscanf_bug_demo() { // sscanf fails scanning the string "" // test data: // line format: username "prj" "desc" (the string in "" can be empty) const char* line1 = "joe \"prj 1\" \"blah foo\""; const char* line2 = "jim \"\" \"quantum leap\""; char un[32], prj[16], desc[256]; int n; bool fErr; // parsing via sscanf: un[0] = 0, prj[0] = 0, desc[0] = 0; // clear them before filling n = sscanf(line1, "%31[^ ] \"%15[^\"]\" \"%255[^\"]\"", un, prj, desc); fErr = n < 3; printf("line1: n=%d : fErr=%d : un=\"%s\" prj=\"%s\" desc=\"%s\"\n", n, fErr, un, prj, desc); un[0] = 0, prj[0] = 0, desc[0] = 0; // clear them before filling n = sscanf(line2, "%31[^ ] \"%15[^\"]\" \"%255[^\"]\"", un, prj, desc); fErr = n < 3; printf("line2: n=%d : fErr=%d : un=\"%s\" prj=\"%s\" desc=\"%s\"\n", n, fErr, un, prj, desc); } int main() { sscanf_bug_demo(); return 0; }