c - Why does fgets return a bad string? -
i don't know how else put it, know i'm doing wrong.
char *temp2= "/shares/mjim"; char ourlist[1024]; fgets(ourlist, 1024, modified) sprintf(sysstring, "grep -c %s %s", ourlist, temp2);
now fgets job, when try form string sprintf grep -c lanuncher.ini
, rest missing. here kicker, if reverse order this:
sprintf(sysstring, "grep -c %s %s", temp2, ourlist);
the result want, , invalid search grep: grep -c /shares/mjim lanucher.ini
also if this:
sprintf(sysstring, "grep -c %s %s", temp2, temp2);
it creates expected string (grep -c /shares/mjim /shares/mjim
). know going on?
well input simple text file single column list:
launcher.ini bits.exe etc....
the garbage in case unusually short string mentioned. when print out ourlist has returns launcher.ini.
fgets
includes trailing '\n'
. want remove before building sysstring ...
fgets(ourlist, sizeof ourlist, modified); size_t ourlistlen = strlen(ourlist); if (ourlistlen && (ourlist[ourlistlen - 1] == '\n')) { ourlist[--ourlistlen] = 0; /* remove trailing '\n' , update ourlistlen */ }
Comments
Post a Comment