You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言中无法从管道读取?编写进程交互读写代码遇问题求助

Interprocess Communication Between input.c and app.c

It looks like you're building a system where input.c interacts with app.c via read/write operations—likely using interprocess communication (IPC) like pipes or redirected standard streams. Here's your code formatted properly, plus some quick context on what each part does:

input.c Code

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>

#define WRITE 1
#define READ 0

void error(char* msg){
    perror(msg);
    exit(-1);
}

void got_here(char* msg){
    printf("Got_here:%s\n",msg);
}

// ... (remaining implementation code goes here)

Key Notes on the Code:

  • The WRITE and READ macros map to standard file descriptor values (1 for stdout/write end, 0 for stdin/read end)—this hints you're using pipe-based IPC or stream redirection between the two processes.
  • The error() function is a clean way to handle runtime issues: it prints a system-level error message with perror() and exits immediately, which is a common pattern in C IPC code.
  • The got_here() function is a simple debugging tool to track execution flow—you can place calls to it at key points to verify which sections of your code are running as expected.

To finish this setup, you'd typically add code to create a pipe, fork a process to launch app.c, and then route data between the two programs using the defined file descriptors. For example, the parent (input.c) might write to the pipe's WRITE end, while the child redirects its stdin to the pipe's READ end before executing app.c.

内容的提问来源于stack exchange,提问作者Wolfat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:41:21