char yes_no = 'z';
int i = 0;
while ( ( yes_no != 'y' ) && ( yes_no != 'n') )
{
read( 0, &yes_no, 1 );
printf("%dA file already exists at your write location. Over write? (y/n)\n", i++);
}
Output looks like this:
0A file already exists at your write location. Overwrite? (y/n)
1A file already exists at your write location. Overwrite? (y/n)
a
2A file already exists at your write location. Overwrite? (y/n)
3A file already exists at your write location. Overwrite? (y/n)
…etc.
Why would it go through the loop twice instead of waiting for user input every time?
When you enter one letter and hit enter it puts
'a\n'into the stream. By reading 1 byte out of that you’re still left with'\n'in the stream, thus next time you callread(), it reads the'\n'instead of blocking and waiting for user input. You need to discard the'\n'that’s left in the stream.