ch8-Copy input to output

Chapter_8 getchar     Exercise_8-1







copy.c     K&R, p. 171         download


#include <stdio.h> // for BUFSIZ 8192
#include <unistd.h> // for read(), write(), fd 0 (stdin), 1 (stdout)

int main() // copy input to output (2 lines)
{
char buf[BUFSIZ]; // 8192 bytes
int n;
// read from file descriptor 0 (stdin) into buf[]
while ((n = read(0, buf, BUFSIZ)) > 0)
{write(1, buf, n);}
// write n bytes from buf[] into file descriptor 1 (stdout)
return 0;
}
/*
gcc copy.c -o copy
./copy
Hello! // Enter
Hello!
Goodbye! // Enter
Goodbye!
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)

./copy < copy.c // redirect input to source file

./copy < copy // redirect input to binary file
*/









Chapter_8 BACK_TO_TOP getchar     Exercise_8-1



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo