unix - How to use fgets() instead of fscanf() on stdin in C? -
i want use fgets instead of fscanf stdin , send child process via pipe. code below works sorting lines in file replacing
fscanf(stdin, "%s", word)
with
fgets(word, 5000, stdin)
gives me warning
warning: comparison between pointer , integer [enabled default]
otherwise program seems work. ideas why getting warning?
int main(int argc, char *argv[]) { pid_t sortpid; int status; file *writetochild; char word[5000]; int count = 1; int sortfds[2]; pipe(sortfds); switch (sortpid = fork()) { case 0: //this child process close(sortfds[1]); //close write end of pipe dup(sortfds[0]); close(sortfds[0]); execl("/usr/bin/sort", "sort", (char *) 0); perror("execl of sort failed"); exit(exit_failure); case -1: //failure fork case perror("could not create child"); exit(exit_failure); default: //this parent process close(sortfds[0]); //close read end of pipe writetochild = fdopen(sortfds[1], "w"); break; } if (writetochild != 0) { //do if parent while (fscanf(stdin, "%s", word) != eof) { fprintf(writetochild, "%s %d\n", word, count); } } fclose(writetochild); wait(&status); return 0; }
fscanf returns int
, fgets char *
. comparision eof results in warning char *
since eof int
.
fgets returns null on eof or error, check that.
Comments
Post a Comment