入力がCでどのように動作するかを学習しています。私の最大の苦労は、端末のEOF動作を理解することです。
まず、WindowsとGCCコンパイラを使用しています。 第二には、私はファイルから入力をリダイレクトしようとしていないよ...私の質問は、Windowsコンソールからの入力についてです私の質問
:私はEOFは、入力ストリームを閉じていることを読んで
、ことをあなたがすることはできませんEOFの後にstdinから読む...これは私の場合ではありません!あなたは^ Zを入力する際にも、奇妙な何かが
WindowsコンソールでのStdinとEOFの動作
int c = 0;
char str[100]={0};
printf("Type in a string with spaces so some chars would remain in Stdin: ");
//Let's say i type "Hello world!"
scanf("%s",str);
while((c=getchar()) != EOF)
printf("%c",c);
//it displays " World!" in console, then i type Enter-^Z-Enter
//The loop exits so far so good
printf("Let's just check if we can still read from stdin, type a char: ");
c = getchar(); //i type the letter 'a'
printf("\nYou entered: %c\n",c); //It displays 'a'?!
起こる:私は明示的に入力し、Ctrl-Zを入力し、私は別getchar関数を()の例...それは標準入力から読み込みを呼び出すない場合は入力した後でも文字列の真ん中では、返される前の任意の文字が返されますが、それが返された後には何も入力されません!しかし、可変内容をチェックすると、-1と等しくないのですか?ここでは例です:
int c = 0;
char str[100]={0};
printf("Type in a string with spaces so some chars would remain in Stdin: ");
//This time i type "Hello wor^Zld!" with ^Z in the middle of "World!"
scanf("%s",str);
while((c=getchar()) != EOF)
printf("%c",c);
//it displays " Wor->" in console, with the cursor hanging waiting for input
/*
So on the one hand, after ^Z everything disappears, but on the other
hand it's waiting for input so it's not EOF?!
*/
//In case you're wondering, here too i can call getchar() and read from stdin!
printf("Let's just check if we can still read from stdin, type a char: ");
c = getchar(); //i type the letter 'a'
printf("\nYou entered: %c\n",c); //It also displays 'a'?!
だから任意の助けをいただければ幸いです...私は本当にこの仕組みを理解しようとしているが、それは本当にCで初心者のために混乱しています私を信じて!
はhttps://msdn.microsoft.com/sv-se/library/xssktc6eを参照してください。aspx about eof –