【C语言】cat实现

源代码来自《C程序设计语言》一书。

#include <stdio.h>





/* cat 函数: 连接多个文件, 版本1 */

int main(int argc, char *argv[])

{

	FILE *fp;

	void filecopy(FILE *, FILE *);



	if( 1 == argc)

		filecopy(stdin, stdout);

	else

		while(--argc > 0)

			if((fp = fopen(*++argv, "r")) == NULL)

			{

				printf("cat:can't  open %s\n", *argv);

				return 1;

			}

			else

			{

				filecopy(fp, stdout);

				fclose(fp);

			}

			return 0;

}



void filecopy(FILE *ifp, FILE *ofp)

{

	int c;



	while((c = getc(ifp)) != EOF)

		putc(c, ofp);

}

 

你可能感兴趣的:(C语言)