0

I have created a c program which requires an input (through scanf). Then I created the .so file and called that in a python script, so that when I run the script, input will be asked in the terminal. But when I run the python program, the terminal hangs.

Please note:
1. My c code

#include <stdio.h>
#include <string.h>
int open(void)
{
    char input[20];
    scanf("input = %s\n",&input);
    printf("\n%s\n","input");
}

2. Command I used for compiling the code

gcc -c usb_comm.c

3.Creating .so file

gcc -shared -o libhello.so usb_comm.o

4.Relevant section of python program
Loading the .so file

from ctypes import cdll
mydll = cdll.LoadLibrary('/home/vineeshvs/Dropbox/wel/workspace/Atmel/libhello.so')


Calling the function defined in the c program

mydll.scanf("%c",mydll.open())

Thanks for listening :)

falsetru
  • 357,413
  • 63
  • 732
  • 636
vineeshvs
  • 479
  • 7
  • 32
  • You may want to read about the [`subprocess` module](http://docs.python.org/2/library/subprocess.html), especially its [`Popen` class](http://docs.python.org/2/library/subprocess.html#popen-constructor). Then re-create your C module as an ordinary program. – Some programmer dude Aug 02 '13 at 09:04

1 Answers1

0

mydll.open() call scanf. Why do you call scanf in Python?

Just call mydll.open() only:

mydll.open()

UPDATE

#include <stdio.h>

void open(void)
{
    char input[20];
    printf("input = "); // If you want prompt
    scanf("%s", input);
    printf("\n%s\n", input);
}
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I replaced the line `mydll.scanf("%c",mydll.open())` with `mdll.open()`. But still the terminal hangs and doesn't ask for the input – vineeshvs Aug 02 '13 at 09:40
  • @vineeshvs, You are misunderstanding about scanf. scanf does not prompt user. – falsetru Aug 02 '13 at 09:42
  • Then which command would you suggest to take input from user? – vineeshvs Aug 02 '13 at 09:44
  • tried the code. It prints `input =` and then waits for input. When I type something (for eg: sdf), it shows the following error `No command 'sdaf' found, did you mean: Command 'sadf' from package 'sysstat' (main) Command 'sdf' from package 'sdf' (universe) sdaf: command not found [2]+ Stopped python first_program.py ` – vineeshvs Aug 02 '13 at 10:32
  • @vineeshvs, I can't reproduce the error. [See this screencast that I just created](http://ascii.io/a/4501). – falsetru Aug 02 '13 at 10:52