标签: stdio

在管道中向后通信

我有一个简单的管道:

node foo.js | node bar.js
Run Code Online (Sandbox Code Playgroud)

bar.js将从 stdin 读取以从foo.js.

但我想要做的是确保在 foo.js 决定退出之前bar.js获取最后一条消息之一foo.js。基本上我想创建一个简单的请求/响应模式。

foo 写入标准输出 --> bar 从标准输入读取 --> bar 如何将消息发送回 foo?

有没有办法在管道中向后通信,或者永远不需要这样做?

pipe stdout stdin stderr stdio

6
推荐指数
2
解决办法
3336
查看次数

终端退格的行为

这与退格 ( \b) 字符的行为有关。我有以下 C 程序:

int main() {
    printf("Hello\b\b");
    sleep(5);
    printf("h\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我终端上的输出是

Helho
Run Code Online (Sandbox Code Playgroud)

随着光标前进到下一行的第一个位置。

首先,整个事情只在 5 秒睡眠后打印,因此我推断从内核到终端的输出是行缓冲的。所以现在,我的问题是:

  1. 既然\b\b回退了两个空格,到了 (second) 的位置l,那么和怎么l被替换的差不多ho应该已经被替换了\n。为什么不是?
  2. 如果我删除 line printf("h\n");,它会打印Hello并返回两个字符,而不会擦除。我从其他答案中得到的这是因为非破坏性退格。为什么输入和输出的这种行为不同?也就是说,如果我在终端中输入一些东西(即使是同一个程序)并按 Backspace,它会擦除​​最后一个字符,但不会擦除输出。为什么?

如果有帮助,我在 xterm 终端上的 Ubuntu 系统上使用 bash。

c terminal tty stdio

5
推荐指数
3
解决办法
5787
查看次数

C 中 Linux 特定 getline() 函数的意外行为

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 1024

void reverse(FILE *, FILE *);

int main(int argc, char ** argv)
{
  ...
  reverse(fptr, stdout);
  ...

  return 0;
}

void reverse(FILE * instream, FILE * outstream)
{
  char ** buf;
  char * lbuf;
  int counter, i;
  size_t slen;

  counter = 0;

  buf = malloc(MAXLEN * sizeof(char));
  if (buf == NULL)
  {
    fputs("malloc failed\n", stderr);
    exit(EXIT_FAILURE);
  }

  lbuf = NULL;
  while ((counter < MAXLEN) && (getline(&lbuf, &slen, instream) != -1))
  {
    buf[counter] = lbuf; …
Run Code Online (Sandbox Code Playgroud)

c glibc stdio

0
推荐指数
1
解决办法
124
查看次数

标签 统计

stdio ×3

c ×2

glibc ×1

pipe ×1

stderr ×1

stdin ×1

stdout ×1

terminal ×1

tty ×1