[C.C++] 写一段附加到进程的主程序

1185 0
狼毛 2023-8-25 20:20:48 | 显示全部楼层 |阅读模式
  1. // 写一段附加到进程的主程序
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>

  8. int main(int argc, char *argv[])
  9. {
  10.     int fd;
  11.     char buf[1024];
  12.     int n;
  13.     if (argc != 2)
  14.     {
  15.         printf("Usage:%s <file>\n", argv[0]);
  16.         exit(1);
  17.     }
  18.     fd = open(argv[1], O_RDWR);
  19.     if (fd < 0)
  20.     {
  21.         perror("open");
  22.         exit(1);
  23.     }
  24.     while (1)
  25.     {
  26.         n = read(fd, buf, sizeof(buf));
  27.         if (n == 0)
  28.         {
  29.             break;
  30.         }
  31.         write(STDOUT_FILENO, buf, n);
  32.     }
  33.     close(fd);
  34.     return 0;
  35. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

狼毛

精英红客

关注
  • 206
    主题
  • 2
    粉丝
  • 1
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行