2021年1月27日星期三

Change input stream mid-program

I have the following program which prints text from a file:

#include<stdio.h>  int main(void) {      int ch;        while ((ch=getchar()) != EOF) {          putchar(ch);      }        printf("Enter a character now...\n");      // possible to prompt input now from a user?      ch = getchar();      printf("The character you entered was: %c\n", (char) ch);    }  

And running it:

$ ./io2 < file.txt  This is a text file  Do you like it?  Enter a character now...  The character you entered was: �  

After this, how would I get a user to enter in a character. For example, if I were to do getchar() (when not redirecting the file to stdin)?

Now it seems it just will keep printing the EOF character if I keep doing getchar() at the end.

https://stackoverflow.com/questions/65910836/change-input-stream-mid-program January 27, 2021 at 07:35AM

没有评论:

发表评论