鱼C论坛

 找回密码
 立即注册
查看: 1495|回复: 1

[已解决]c读取文件达到scanf的等待效果

[复制链接]
发表于 2021-5-10 10:42:05 | 显示全部楼层 |阅读模式
10鱼币
我想要让c程序一直读取文件的内容,如果有数据就读取,如果没数据就等待,达到scanf类似的效果,我知道能够用while循环一直判断来实现但是这样太耗cpu了,问下有没更好的解决方法。其实我想要实现的功能是两个c程序中的通讯,想socket,内存共享这些想着小题大做了,我只需要在本地实现两个c程序互相传输数据就行了,如果哪个大佬能够这样从根本帮我解决,我会非常感谢的。
最佳答案
2021-5-10 10:42:06
可以试试 命名管道

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>

  6. // 父进程负责从标准输入读取数据,然后写到fifo
  7. // 子进程负责从fifo读取数据,然后写到标准输出
  8. int main(int argc, char *argv[]) {
  9.     if(argc != 2) return -1;
  10.     int fd[2];
  11.     if(!strcmp(argv[1], "a")) {
  12.         fd[0] = open("fifo_0", O_RDONLY);
  13.         fd[1] = open("fifo_1", O_WRONLY);
  14.     } else if(!strcmp(argv[1], "b")) {
  15.         fd[1] = open("fifo_0", O_WRONLY);
  16.         fd[0] = open("fifo_1", O_RDONLY);
  17.     } else return -1;
  18.     if(!fork()) {
  19.         while(1) {
  20.             char buff[1024];
  21.             ssize_t nbyte = read(fd[0], buff, 1024);
  22.             write(1, buff, nbyte);
  23.             if(nbyte == 0 || !strncmp(buff, "exit", 4)) {
  24.                 close(fd[0]); close(fd[1]); exit(0);
  25.             }
  26.         }
  27.     }
  28.     while(1) {
  29.         char buff[1024];
  30.         ssize_t nbyte = read(0, buff, 1024);
  31.         write(fd[1], buff, nbyte);
  32.         if(nbyte == 0 || !strncmp(buff, "exit", 4)) {
  33.             close(fd[0]); close(fd[1]); exit(0);
  34.         }
  35.     }
  36.     return 0;
  37. }
复制代码


1.png

GIF.gif

最佳答案

查看完整内容

可以试试 命名管道
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-10 10:42:06 | 显示全部楼层    本楼为最佳答案   
可以试试 命名管道

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>

  6. // 父进程负责从标准输入读取数据,然后写到fifo
  7. // 子进程负责从fifo读取数据,然后写到标准输出
  8. int main(int argc, char *argv[]) {
  9.     if(argc != 2) return -1;
  10.     int fd[2];
  11.     if(!strcmp(argv[1], "a")) {
  12.         fd[0] = open("fifo_0", O_RDONLY);
  13.         fd[1] = open("fifo_1", O_WRONLY);
  14.     } else if(!strcmp(argv[1], "b")) {
  15.         fd[1] = open("fifo_0", O_WRONLY);
  16.         fd[0] = open("fifo_1", O_RDONLY);
  17.     } else return -1;
  18.     if(!fork()) {
  19.         while(1) {
  20.             char buff[1024];
  21.             ssize_t nbyte = read(fd[0], buff, 1024);
  22.             write(1, buff, nbyte);
  23.             if(nbyte == 0 || !strncmp(buff, "exit", 4)) {
  24.                 close(fd[0]); close(fd[1]); exit(0);
  25.             }
  26.         }
  27.     }
  28.     while(1) {
  29.         char buff[1024];
  30.         ssize_t nbyte = read(0, buff, 1024);
  31.         write(fd[1], buff, nbyte);
  32.         if(nbyte == 0 || !strncmp(buff, "exit", 4)) {
  33.             close(fd[0]); close(fd[1]); exit(0);
  34.         }
  35.     }
  36.     return 0;
  37. }
复制代码


1.png

GIF.gif
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 08:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表