سؤال

I am trying to establish a connection between TCL script and C code.

here is the TCL script

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

here is the C code

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);

The output looks like:

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

I don't understand, what could be the reason for the garbage line ???

appreciate your precise and earlier help.

thanks and Regards, M.

هل كانت مفيدة؟

المحلول

First, as Jerry pointed out, you need a space between the variable fs and the square bracket:

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

I don't know the reason why you read file in such a low-level manner (i.e. using file number, instead of the FILE* handle). However, you will need to terminate your string yourself since read() does not automatically do so:

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top