fgets and dealing with CTRL+D input

时光毁灭记忆、已成空白 提交于 2019-12-19 09:47:59

问题


I am grabbing some standard input from the user and if the user presses CTRL+D, I want to display an error and terminate the program. I think perhaps my issue may be related to being stuck in a while loop;

int readInput(){
   char buff[10];
   int count = 0;
   int counter;
   printf("Enter random number: ");
   fgets(buff, 10, stdin);
   if ((int) strtol(buff, NULL, 10) == 0){
      printf("Error reading number. \n");
      return 0;   //This will get hit if the user presses CTRL+D at this input.
   }
   counter = atol(buff);
   while (count < counter){ 
      printf("Enter a label: ");
      fgets(buff, 10, stdin);
      if ((int) strtol(buff, NULL, 10) == 0){
         printf("Error reading label");
         return 0;  //This will not get hit if the user presses CTRL+D at this input, but why?
         //I've also tried assigning a variable to 0, breaking out of the loop using break; and returning the variable at the end of the function but that also does not work.

      //the rest of the while loop continues even if user hit CTRL+D
      printf("Enter Value: " );
      fgets(buff, 10, stdin);
      //..rest of while loop just gets other inputs like above
      count++;
   }

//termination happens in main, if readInput returns a 0 we call RETURN EXIT_FAILURE;

I don't understand why at the first input if the user presses CTRL+D, the program responds accordingly but the second time it completely ignores it.


回答1:


On Linux, Ctrl + D generates EOF, so you need to check the return value of fgets() every time. When EOF is encountered, fgets() returns a null pointer

if (fgets(buff, 10, stdin) == NULL)
    print_error();


来源:https://stackoverflow.com/questions/19228645/fgets-and-dealing-with-ctrld-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!