gpt4 book ai didi

c - TCL 和 C 中的读/写 FIFO,垃圾输出

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:50 25 4
gpt4 key购买 nike

我正在尝试在 TCL 脚本和 C 代码之间建立连接。

这是TCL脚本

set fs[open "./fifo_server" "w"]
puts $fs "level_3"
flush $fs

这是C代码

if ((fs = fopen ("./fifo_server", "r"))== NULL)
perror ("error occured while opening FIFO_SERVER");
else {
fs1 = fileno(fs);
read(fs1, in_data, sizeof(in_data));
}
printf ("in_data = %s\n", in_data);

输出如下:

in_data = level_3
(some garbage stuff 5 spaces which contains Question marks, Squares,
Characters etc.)

我不明白,垃圾行可能是什么原因???

感谢您准确和早期的帮助。

感谢和问候,

最佳答案

首先,正如 Jerry 指出的那样,您需要在变量 fs 和方括号之间留一个空格:

set fs [open "./fifo_server" "w"]

我不知道您为什么以如此低级的方式读取文件(即使用文件编号,而不是 FILE* 句柄)。但是,您需要自己终止字符串,因为 read() 不会自动这样做:

int chars_read; /* How many chars read from a file */

if ((fs = fopen ("./fifo_server", "r")) == NULL)
perror ("error occured while opening FIFO_SERVER");
else {
fs1 = fileno(fs);
chars_read = read(fs1, in_data, sizeof(in_data));
in_data[chars_read] = '\0'; /* terminate your string */
}
printf ("in_data = %s\n", in_data);

关于c - TCL 和 C 中的读/写 FIFO,垃圾输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21705396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com