我正在尝试在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.)
我不明白,垃圾线可能是什么原因???
感谢您的精确和早期帮助。
谢谢和问候, 微米。
答案 0 :(得分:2)
首先,正如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);