C - Write a version of cat using UNIX system access
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net
/*
* Write a version of cat using UNIX system access
*
* Cat.c - by FreeMan
*/
#include <stdio.h>
#include <fcntl.h>
#define BUFSIZE 1024
int main(int argc, char *argv[])
{
int fd;
void filecopy(int f, int t);
if (argc == 1)
{
filecopy(0, 1);
}
else
{
while (--argc > 0)
{
if ((fd = open(*++argv, O_RDONLY, 0)) == -1)
{
printf("cat: can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fd, 1);
close(fd);
}
}
}
return 0;
}
void filecopy(int from, int to)
{
int n;
char buf[BUFSIZE];
while ((n = read(from, buf, BUFSIZE)) > 0)
{
write(to, buf, n);
}
}
还没有评论,来说两句吧...