-2

The python script contains lots of libraries imported

My C code so far:

#include <stdio.h>
#include <python2.7/Python.h>

void main(int argc, char *argv[])
{    
    FILE* file;

    Py_SetProgramName(argv[0]);
    Py_Initialize();    
    PySys_SetArgv(argc, argv);

    file = fopen("analyze.py","r");

    PyRun_SimpleFile(file, "analyze.py");
    Py_Finalize();                      

    return;
}

Is there any other way that I can use so that even if any modification in arguments or number of python scripts I call inside c program increases the same code can be used with little changes?

Can I use system call and use the result obtained from it?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
aAAAAAAa
  • 135
  • 1
  • 2
  • 9
  • Please edit your question and fix the code formatting. – Lundin Mar 24 '17 at 10:52
  • Try looking at your own post for a few seconds and you should be able to tell... oh ffs I'll fix it for you. – Lundin Mar 24 '17 at 12:27
  • 1) paste your code into the post 2) while it is marked, click on the code formatting icon `{}`. This will give the code correct syntax coloring and change font to Courier. – Lundin Mar 24 '17 at 14:24

2 Answers2

1

One useful way is calling a python function within c, which is that you need instead of execute whole script.

As described in >> here

Community
  • 1
  • 1
obayhan
  • 1,636
  • 18
  • 35
0

You can do like this to call the python file from the C program:

char command[50] = "python full_path_name\\file_name.py";

system(command);

This piece of code worked for me...

I didn't use # include < python2.7/Python.h>

You can write the results from the python file to any text file and then use the results stored in the text file to do whatever you want to do...

You can also have a look at this post for further help:

Calling python script from C++ and using its output

Community
  • 1
  • 1
Siladittya
  • 1,156
  • 2
  • 13
  • 41
  • Try using `#!usr/bin/python` at the beginning of the python script – Siladittya Mar 24 '17 at 11:49
  • I didn't use version... I was using python 2.7... and this worked for me – Siladittya Mar 24 '17 at 14:43
  • I guess you can use `argparse` in your python script... And in the C code you can edit the `command` string like this: `char command[100] = 'python full_path_name\\file_name.py arg1 arg2 ..argn'` – Siladittya Mar 25 '17 at 12:01
  • You can also use `import sys` in your python script and use `sys.argv[i]` to access the corresponding arguments where `i` is the position of the argument in the command line – Siladittya Mar 25 '17 at 12:08
  • You can have a look at this post http://stackoverflow.com/questions/125828/capturing-stdout-from-a-system-command-optimally?noredirect=1&lq=1 – Siladittya Mar 25 '17 at 16:45
  • and this one http://stackoverflow.com/questions/23156620/c-programming-handling-stdout-and-stdin-using-pipes?rq=1 – Siladittya Mar 25 '17 at 16:59
  • I saw your other posts.. :| .... you can just concatenate strings to include the arguments in `command` in which case you have to take the length of the buffer taking into consideration the argument lengths in string format... I hope you are getting me?? I am a beginner as well... -__- – Siladittya Mar 25 '17 at 17:36