Chapter 27 . (Yahoo free web hosting) Programming Environments and Interfaces 711

Chapter 27 . Programming Environments and Interfaces 711 Listing 27-2: Reading and Writing to stdin and stdout /* * readkey.c - reads characters from stdin */ #include int main(int argc, char *argv[]) { int c, i = 0; /* read characters until newline read */ printf( INPUT: ); while ((c = getchar()) != n ) { ++i; putchar(c); } printf( ncharacters read: %dn , i + 1); return 0; } To compile this program, use the following command: $ gcc readkey.c -o readkey readkey.c reads input from stdin until it encounters a newline (such as pressing the Enter key). Then it displays the text entered and the number of characters read (the count includes the newline) and exits. Here s how it works: $ ./readkey INPUT: There are three primary means of creating programs that interact with users at the command line There are three primary means of creating programs that interact with users at the command line characters read: 96 The text wraps oddly because of this book s formatting constraints. You can also feed readkey.c input from stdin using the cat command: $ cat /etc/passwd | ./readkey INPUT: root:x:0:0::/root:/bin/bash characters read: 28 In this case, you only see the first line of /etc/passwd because each line of the file ends with a newline. It should be clear that programmatically interacting with the command line is simple, but not terribly user-friendly or attractive.
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Leave a Reply