473,373 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

pass a text file , as argument of main() in Python

Hallo everyone,
I'm trying to convert a main function from C to Python,by using Swig and call it with a .txt argument.The code is the following :
Expand|Select|Wrap|Line Numbers
  1. /*test.c*/
  2. #include <stdio.h>
  3. int main(int argc, char ** argv)
  4. {
  5.     float f1,f2,f3;
  6.     FILE *fp;
  7.     fp=fopen(argv[1],"r");
  8.     if(fp!=NULL)
  9.     {
  10.         printf("Succesfull opening\n");
  11.         fscanf(fp,"%f\n%f\n%f",&f1,&f2,&f3);
  12.         printf("the values are \n%f\n%f\n%f\n",f1,f2,f3);
  13.         fclose(fp);
  14.     }
  15.     return 0;
  16. }
  17.  
  18. /*test_interface.i*/
  19. %module example
  20. %{
  21.     int main(int argc,char **argv);
  22. %}
  23. int main(int argc,char **argv);
  24.  
  25. /*text.txt*/
0.2313
2.3123
12.123

from Linux command prompt I execute the following commands:

$ swig -python test_interface.i
$ gcc -c test.c test_interface_wrap.c -I/usr/include/python2.5
$ ld -shared test.o test_interface_wrap.o -o _example.so
$ python
>>>from _example import *
>>>main(1,'text1.txt') // ERROR !!!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'main', argument 2 of type 'char **'

All i want to do is the same thing like the following C procces:
$ gcc test.c -o test
$ ./test text.txt
the values are
0.231300
2.312300
12.123000

What is the solution?
Aug 17 '07 #1
7 7328
varuns
39
Hallo everyone,
I'm trying to convert a main function from C to Python,by using Swig and call it with a .txt argument.The code is the following :

/*test.c*/
#include <stdio.h>
int main(int argc, char ** argv)
{
float f1,f2,f3;
FILE *fp;
fp=fopen(argv[1],"r");
if(fp!=NULL)
{
printf("Succesfull opening\n");
fscanf(fp,"%f\n%f\n%f",&f1,&f2,&f3);
printf("the values are \n%f\n%f\n%f\n",f1,f2,f3);
fclose(fp);
}
return 0;
}

/*test_interface.i*/
%module example
%{
int main(int argc,char **argv);
%}
int main(int argc,char **argv);

/*text.txt*/
0.2313
2.3123
12.123

from Linux command prompt I execute the following commands:

$ swig -python test_interface.i
$ gcc -c test.c test_interface_wrap.c -I/usr/include/python2.5
$ ld -shared test.o test_interface_wrap.o -o _example.so
$ python
>>>from _example import *
>>>main(1,'text1.txt') // ERROR !!!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'main', argument 2 of type 'char **'

All i want to do is the same thing like the following C procces:
$ gcc test.c -o test
$ ./test text.txt
the values are
0.231300
2.312300
12.123000

What is the solution?
hi
just post your test_interface_wrap.c file

varun
Aug 17 '07 #2
hallo varun
Thanks for your answer .test_interface_wrap.c is about 2.700 lines .it is helpfull to post it?
Aug 21 '07 #3
varuns
39
hallo varun
Thanks for your answer .test_interface_wrap.c is about 2.700 lines .it is helpfull to post it?
only post lines of function
SWIG_wrap_main(...) or something like that
Aug 21 '07 #4
Hallo again verun
I hope the following code of interface_wrap.c is helpful.Thank you again.
Expand|Select|Wrap|Line Numbers
  1. extern "C" {
  2. #endif
  3. SWIGINTERN PyObject *_wrap_main(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  4.   PyObject *resultobj = 0;
  5.   int arg1 ;
  6.   char **arg2 = (char **) 0 ;
  7.   int result;
  8.   int val1 ;
  9.   int ecode1 = 0 ;
  10.   void *argp2 = 0 ;
  11.   int res2 = 0 ;
  12.   PyObject * obj0 = 0 ;
  13.   PyObject * obj1 = 0 ;
  14.  
  15.   if (!PyArg_ParseTuple(args,(char *)"OO:main",&obj0,&obj1)) SWIG_fail;
  16.   ecode1 = SWIG_AsVal_int(obj0, &val1);
  17.   if (!SWIG_IsOK(ecode1)) {
  18.     SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "main" "', argument " "1"" of type '" "int""'");
  19.   } 
  20.   arg1 = (int)(val1);
  21.   res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_p_char, 0 |  0 );
  22.   if (!SWIG_IsOK(res2)) {
  23.     SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "main" "', argument " "2"" of type '" "char **""'"); 
  24.   }
  25.   arg2 = (char **)(argp2);
  26.   result = (int)main(arg1,arg2);
  27.   resultobj = SWIG_From_int((int)(result));
  28.   return resultobj;
  29. fail:
  30.   return NULL;
  31. }
  32.  
  33.  
  34. static PyMethodDef SwigMethods[] = {
  35.      { (char *)"main", _wrap_main, METH_VARARGS, NULL},
  36.      { NULL, NULL, 0, NULL }
  37. };
  38.  
  39.  
  40. /* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */
  41.  
  42. static swig_type_info _swigt__p_char = {"_p_char", "char *", 0, 0, (void*)0, 0};
  43. static swig_type_info _swigt__p_p_char = {"_p_p_char", "char **", 0, 0, (void*)0, 0};
  44.  
  45. static swig_type_info *swig_type_initial[] = {
  46.   &_swigt__p_char,
  47.   &_swigt__p_p_char,
  48. };
  49.  
  50. static swig_cast_info _swigc__p_char[] = {  {&_swigt__p_char, 0, 0, 0},{0, 0, 0, 0}};
  51. static swig_cast_info _swigc__p_p_char[] = {  {&_swigt__p_p_char, 0, 0, 0},{0, 0, 0, 0}};
  52.  
  53. static swig_cast_info *swig_cast_initial[] = {
  54.   _swigc__p_char,
  55.   _swigc__p_p_char,
  56. };
  57.  
  58.  
  59. /* -------- TYPE CONVERSION AND EQUIVALENCE RULES (END) -------- */
  60.  
  61. static swig_const_info swig_const_table[] = {
  62. {0, 0, 0, 0.0, 0, 0}};
  63.  
  64. #ifdef __cplusplus
  65. }
Aug 22 '07 #5
varuns
39
Hallo again verun
I hope the following code of interface_wrap.c is helpful.Thank you again.

extern "C" {
#endif
SWIGINTERN PyObject *_wrap_main(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
int arg1 ;
char **arg2 = (char **) 0 ;
int result;
int val1 ;
int ecode1 = 0 ;
void *argp2 = 0 ;
int res2 = 0 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;

if (!PyArg_ParseTuple(args,(char *)"OO:main",&obj0,&obj1)) SWIG_fail;
ecode1 = SWIG_AsVal_int(obj0, &val1);
if (!SWIG_IsOK(ecode1)) {
SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "main" "', argument " "1"" of type '" "int""'");
}
arg1 = (int)(val1);
res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_p_char, 0 | 0 );
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "main" "', argument " "2"" of type '" "char **""'");
}
arg2 = (char **)(argp2);
result = (int)main(arg1,arg2);
resultobj = SWIG_From_int((int)(result));
return resultobj;
fail:
return NULL;
}


static PyMethodDef SwigMethods[] = {
{ (char *)"main", _wrap_main, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};


/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */

static swig_type_info _swigt__p_char = {"_p_char", "char *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_p_char = {"_p_p_char", "char **", 0, 0, (void*)0, 0};

static swig_type_info *swig_type_initial[] = {
&_swigt__p_char,
&_swigt__p_p_char,
};

static swig_cast_info _swigc__p_char[] = { {&_swigt__p_char, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_p_char[] = { {&_swigt__p_p_char, 0, 0, 0},{0, 0, 0, 0}};

static swig_cast_info *swig_cast_initial[] = {
_swigc__p_char,
_swigc__p_p_char,
};


/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (END) -------- */

static swig_const_info swig_const_table[] = {
{0, 0, 0, 0.0, 0, 0}};

#ifdef __cplusplus
}


hi
sorry for the late responce
i think you need to manually write for (int argc,char *argv[])
try this code in your .i file

Expand|Select|Wrap|Line Numbers
  1. %typemap(in) (int argc, char *argv[]) {
  2.   int i;
  3.   if (!PyList_Check($input)) {
  4.     PyErr_SetString(PyExc_ValueError, "Expecting a list");
  5.     return NULL;
  6.   }
  7.   $1 = PyList_Size($input);
  8.   $2 = (char **) malloc(($1+1)*sizeof(char *));
  9.   for (i = 0; i < $1; i++) {
  10.     PyObject *s = PyList_GetItem($input,i);
  11.     if (!PyString_Check(s)) {
  12.         free($2);
  13.         PyErr_SetString(PyExc_ValueError, "List items must be strings");
  14.         return NULL;
  15.     }
  16.     $2[i] = PyString_AsString(s);
  17.   }
  18.   $2[i] = 0;
  19. }
  20.  
  21. %typemap(freearg) (int argc, char *argv[]) {
  22.    if ($2) free($2);
  23. }
Aug 24 '07 #6
I try to and it works. Thank you a lot varuns.
Aug 31 '07 #7
Hello, varuns.
I also meet a similar question, I don't know how to deal with the arguments of the main() function, should I replace the main() function in the c file? Thanks
Mar 19 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: timothy.williams | last post by:
Hi. I trying to write an extension module to call some C libraries so I can use them in Python. Several of the library functions pass pointers to structures as arguments. I was thinking that I...
7
by: Morgan | last post by:
I have read much posts on the argument but no one clearly says if this operation is possible or not. Simply I have a routine which reads from a text file some integer arrays (1 or 2D). The...
4
by: tony.ha | last post by:
Hello, I have open a Python program in the IDLE, but when I select the "run module" under "run" menu, it does not allow me to pass an argument to my Python program! How do you pass an argument...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
3
by: erikcw | last post by:
Hi, I'm getting the following error when I try to pass a list into a function. My List: crea = Traceback (most recent call last): File "wa.py", line 118, in ? curHandler.walkData()
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.