c - Having issues with fwrite and fgets processing it -
char string[50], s[50]; file *f = tmpfile(); count = 1; while (fgets(string, 50, stdin)) { if (string[0] == '!') { rewind(f); } else { fwrite(string, 50, 1, f); } if (strcmp("history\n", string) == 0) { rewind(f); while(fgets(s, 50, f)) { printf("\t%d %s", count, s); count++; } count = 1; } } the context of code not hugely important. problem let's fgets takes in "ls", "date", , "history". resulting output is:
1 ls 2 3 te 4 5 ory 6 it should be:
1 ls 2 date 3 history
since feature requests mark comment answer remain declined, copy above solution here.
looks '\r's in buffer. , should fwrite strlen(string) bytes. – daniel fischer
Comments
Post a Comment