char s;
list<char> inp;
while(s=fgetc(stdin),s!=EOF)
{
inp.push_back(s);
printf("%c",s);
}
for(list<char>::iterator n=inp.begin();n!=inp.end();n++)
{
cout<<*n;
}
The while loop in the above code is never being terminated ? This may happen if EOF is never recieved from stdin. How can this happen?
Make
sanint. Thefgetc()function returns anint, sinceEOFis defined as one.Change it to:
Also,
EOFis not read automatically when usingstdinunless you are using a pipe.Think about it this way…
The program waits for the user to type in characters. How would the program know if
EOFhas been reached or the user is thinking about what to type next. The user has to sendEOFto the program by typingctrl+D.The exception is when you have redirected
stdinto a pipe. For example, if you compiled your program toa.out: