0

fscanf の代わりに fgets を使用して stdin を取得し、それをパイプ経由で子プロセスに送信したいと考えています。以下のコードは、ファイル内の行をソートするために機能しますが、置き換えます

fscanf(stdin, "%s", word)

fgets(word, 5000, stdin)

私に警告を与える

warning: comparison between pointer and integer [enabled by default]

それ以外の場合、プログラムは機能しているようです。警告が表示される理由はありますか?

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 is the child process
      close(sortFds[1]); //close the write end of the 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 to fork case
      perror("Could not create child");
      exit(EXIT_FAILURE);
    default: //this is the parent process
      close(sortFds[0]); //close the read end of the pipe
      writeToChild = fdopen(sortFds[1], "w");
      break;
  }

  if (writeToChild != 0) { //do this if you are the parent
    while (fscanf(stdin, "%s", word) != EOF) {
      fprintf(writeToChild, "%s %d\n",  word, count);
    }   
  }  

  fclose(writeToChild);

  wait(&status);

  return 0;
}
4

2 に答える 2

4

fscanf は an を返しint、fgets を取得しますchar *。EOF と比較すると、char *EOF は であるため、警告が表示されintます。

fgets は EOF またはエラーで NULL を返すので、それを確認してください。

于 2013-05-12T23:36:30.923 に答える