Re: [newbie]C code does not Run.
Re: [newbie]C code does not Run.
On Wed, Oct 09, 2002 at 11:26:36PM +0900, kunikyo wrote:
>
int main(int argc, const char *argv[])
>
{
>
int c;
>
>
c = getchar();
>
while (c != EOF) {
>
putchar(c);
>
c = getchar();
>
}
>
>
}
>
>
Build is OK. But this code does not work. I input a character by
>
keyboard but this
>
code does not work. Nothing was displayed.
You have a few issues which should exist regardless of platform (not
Mac OS X-specific):
First you need the line:
#include <stdio.h>
at the beginning of your program otherwise the definition for EOF will
not be included and the program should refuse to compile. Nor will
those for putchar or getchar, but without -Wall or equivalent, gcc
will not report them.
Second, program input and output is line buffered by the terminal, so
you will only see a result after you press return. To turn this off,
you can use 'stty -icanon', and if you want to turn off echo too, so
you only see one copy of the characters, 'stty -icanon -echo'. 'stty
cooked' will return to a normal setting thereafter. So you could run
your program from Terminal like this:
% ./sample
testing
testing
% stty -icanon; ./sample; stty cooked
tteessttiinngg
^D^C
% stty -icanon -echo; ./sample; stty cooked
testing
%
As you notice, ^D does not work for EOF, I don't know enough about
Unix terminal stuff to know whether it's possible to turn EOF
processing back on while leaving the terminal in character mode.
These types of questions are probably not appropriate to a Cocoa
developer list though, since they don't concern Cocoa and are
applicable to any unix-like platform.
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.