Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
asking for expect which used to interact program?

Posted by lanjun (lanjun), 2 November 2006
I want to use a script file to control a c program. The file is flowing:

test.exp file:

set timeout 1
log_file "test.txt"
spawn test

#send "123\r"

#the paragram which needed by test program is stored in test1.txt file.
set output [open test1.txt r]
set f [read $output]
send $f
close $output

#send "75,63,98\r"
expect eof

test.c file:
#include <stdio.h>
 int a,b,c,d;
     printf ("input a :\n");
     scanf ("%d",&a);
     printf ("%d\n",a);
   }while (123!=a);
 printf ("Please input b,c,d:\n");
 scanf ("%d,%d,%d", &b,&c,&d);

text1.txt file:

ok,lets run it.and get the result:
spawn test
78,50,96input a:
Please input b,c,d:

As you know, i expect to get this result:
input a:
Please input b,c,d:

why??why i can't get the right result i expectedit makes me felt very wondering. can you give me some wonderfull advice for that.
thank you very much!!!!

Posted by admin (Graham Ellis), 2 November 2006
When you input the value to the C program, it echos back the input you made and you need to suck that up with your expect program.

Posted by lanjun (lanjun), 3 November 2006
thanks for your kindness to reply my stupid question.
"suck that up with your expect program", i'm sorry i cann't understand it very clearly. do you mean i must write something to get the c program's return value?
en, let me it possible? beacuse i cann't find one exppect command for that function. and do you have a good idea?

Posted by admin (Graham Ellis), 3 November 2006
Your expect must also read back your inputs which are echoed by the server.  In  addition, you may like to clean up your display by setting log_user to 0.  

Posted by lanjun (lanjun), 3 November 2006
well, yeah, you are right. send inputs must be twice. so i get the right return value... ; expect language is so wondderful.  i hope i can control it one day...hehe,that heared a little long..
thanks,Graham Ellis!!!

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho