0

I have the following code which basically acts as a typewriter:

int main ()
{
	char c;
	puts ("Enter text:");
	do {
		c=getchar();
		putchar (c);
	} while (!feof(stdin));
	return 0;
}

However when I feed it a text file for input: $ ./typewriter < input.txt it always adds a ? at the end of the output. I'm not sure why this is happening, can anyone please point me to a reason why?

Thanks in advance!

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Reprise
2

Probably because that code prints the EOF character. Try this one. Variable C needs to be int, not char.

int c;
while( (c = getchar()) != EOF)
   putchar(c);
0

Probably because that code prints the EOF character. Try this one. Variable C needs to be int, not char.

int c;
while( (c = getchar()) != EOF)
   putchar(c);

Ah, perfect. Thank you!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.