无法读取使用read()系统调用从终端的数据 [英] Not able to read data from other terminal using read() system call

查看:132
本文介绍了无法读取使用read()系统调用从终端的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在伪终端在以下code 的/ dev / PTS / 1 ,我试着从终端读取内容的/ dev / PTS / 2

Hi everyone I am running the following code on pseudo terminal /dev/pts/1 and I am tryin to read the contents from the terminal /dev/pts/2.

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main(){

char str[50];

int fd = open("/dev/pts/2",O_RDONLY);
str[read(fd,str,20)] = '\0';

printf("%s\n",str);


return 0;

}

anirudh@anirudh-Aspire-5920:~$ gcc test.c
anirudh@anirudh-Aspire-5920:~$ ./a.out
n
anirudh@anirudh-Aspire-5920:~$ 

在终端的/ dev / PTS / 2 我输入了anirudh,然而这表明airudh关于这一点,缺少的人物ñ 被显示在终端上的/ dev / PTS / 1
然而,当我试图从终端读取的/ dev / PTS / 1 我可以正常读取每一个字符。
所以我不能够理解这个程序的行为。请帮助我。提前致谢。 :)

On the terminal /dev/pts/2 I had typed "anirudh" however it showed "airudh" on that and the missing character n was displayed on the terminal /dev/pts/1. However when I try to read from the terminal /dev/pts/1 I can read every character properly. So I am not able to understand the behavior of this program. Please help me out. Thanks in advance. :)

推荐答案

首先,你可能有另一个进程从/ dev读/ PTS / 2,因此字符发送到它,不是你的。然后,终端在读取可能设置字符每字符模式下通过其他方法(这就是一些shell),你正在阅读一个字符。

First, you probably have another process reading from /dev/pts/2 and thus characters are send to it and not yours. Then the terminal is probably set in read "char per char" mode by that other process (that's what some shell do), you are reading just one character.

这篇关于无法读取使用read()系统调用从终端的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆