diff --git a/m-string.h b/m-string.h index fe7c2eb8..4667dd30 100644 --- a/m-string.h +++ b/m-string.h @@ -657,6 +657,7 @@ string_fget_word (string_t v, const char separator[], FILE *f) { char buffer[128]; char c = 0; + int d; STRINGI_CONTRACT(v); assert (f != NULL); assert (1+20+2+strlen(separator)+3 < sizeof buffer); @@ -664,6 +665,14 @@ string_fget_word (string_t v, const char separator[], FILE *f) v->size = 0; v->ptr[0] = 0; bool retcode = false; + /* Skip separator first */ + do { + d = fgetc(f); + if (d == EOF) { + return false; + } + } while (strchr(separator, d) != NULL); + ungetc(d, f); /* NOTE: We generate a buffer which we give to scanf to parse the string, that it is to say, we generate the format dynamically! The format is like " %49[^ \t.\n]%c" diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 6f0e0a70..0501e4f5 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -409,7 +409,7 @@ static void test0(void) string_init(s1); f = fopen("a-mstring.dat", "wt"); assert(f != NULL); - fprintf(f, "hello world.\n\tHowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww do you do?"); + fprintf(f, "hello world...\n\tHowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww do you do?"); fclose(f); f = fopen("a-mstring.dat", "rt"); b = string_fget_word(s1, " \t.\n?", f);