C - Write a version of cat using UNIX system access

小咪咪 2022-11-16 13:50 193阅读 0赞

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

  1. /*
  2. * Write a version of cat using UNIX system access
  3. *
  4. * Cat.c - by FreeMan
  5. */
  6. #include <stdio.h>
  7. #include <fcntl.h>
  8. #define BUFSIZE 1024
  9. int main(int argc, char *argv[])
  10. {
  11. int fd;
  12. void filecopy(int f, int t);
  13. if (argc == 1)
  14. {
  15. filecopy(0, 1);
  16. }
  17. else
  18. {
  19. while (--argc > 0)
  20. {
  21. if ((fd = open(*++argv, O_RDONLY, 0)) == -1)
  22. {
  23. printf("cat: can't open %s\n", *argv);
  24. return 1;
  25. }
  26. else
  27. {
  28. filecopy(fd, 1);
  29. close(fd);
  30. }
  31. }
  32. }
  33. return 0;
  34. }
  35. void filecopy(int from, int to)
  36. {
  37. int n;
  38. char buf[BUFSIZE];
  39. while ((n = read(from, buf, BUFSIZE)) > 0)
  40. {
  41. write(to, buf, n);
  42. }
  43. }

发表评论

表情:
评论列表 (有 0 条评论,193人围观)

还没有评论,来说两句吧...

相关阅读