473,327 Members | 2,094 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,327 software developers and data experts.

Exchange Data between Python and C++

I have just started learning programming. I have completed a number of tutorials with regards to Python Basics.

I now want to exchange information between python and C++. For example. I have 2 numbers in python which I call a program written in C, which adds those 2 numbers and returns the result to python.

Which is the simplest way to do this?
May 13 '15 #1
3 2909
computerfox
276 100+
Do you mean that you are getting the numbers by calling a C program from your Python code?
If you are printing the numbers in the C code, you can probably write it to a temporary file, gather the data in the Python code, then remove the temporary file.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/python
  2. import os
  3.  
  4. os.system("./cprog > temp.txt");
  5. raw_data=open("temp.txt").read();
  6. os.system("rm temp.txt");
  7. print raw_data;
  8.  
And you can make that work for other programs:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/python
  2. import os
  3.  
  4. def readc(cpath):
  5.  os.system("'"+cpath+"' > temp.txt");
  6.  raw_data=open("temp.txt").read();
  7.  os.system("rm temp.txt");
  8.  return raw_data;
  9.  
  10. print readc("cprog");
  11.  
http://safe.abelgancsos.com/codepost...ect.php?id=381

http://safe.abelgancsos.com/codepost...ect.php?id=382
May 21 '15 #2
Hi thanks for your response!

I have already successfully tried this method of reading from a text file and even saving it to a different one. My problem and lack of understanding focuses on 2 points:

1) How can i return the value to the python program
2) how to directly send data between the 2 programs? I mean how to call the C++ function and send the arguments directly from the python program rather than having the function to read it from a text file?

Thanks again for your help
May 21 '15 #3
dwblas
626 Expert 512MB
There is not enough information here. You can use subprocess to run a program and supply command line arguments, as well as retrieve anything printed, but I don't know if that is what you mean. See Doug Hellmann's Python Module of the Week on subprocess to see if it is what you want to do http://pymotw.com/2/subprocess/index...ule-subprocess
May 25 '15 #4

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

Similar topics

0
by: Richard Morey | last post by:
Hi, Our company currently has a SQL Server 2000 database with various client information, accessed through an ASP driven intranet. We also use Exchange 2000 and a shared contacts folder. Many...
10
by: jeff | last post by:
I've been doing some minor stuff with an Access Database that creates an email, or a calendar item etc. for a couple of years now. I have had no problem using the MAPI code and haven't had any...
2
by: yangxiaoxiang | last post by:
I find there are some conditions I can't get data defined in another class. One condition is: namespace Test { class A { private int aValue; public void method1() { B b=new B;
1
by: Usha | last post by:
Hi I would like to know if it is possible to access the calendar data from MS exchange server 2000 from an ASP.NET application. If possible, please provide reference to relevant articles/code...
1
by: Dimitris Pantazopoulos | last post by:
Hi How can I exchange data between two ASPX pages without a round-trip? Let's say from the 1st page I call the 2nd one in a new window and then I want to return back a value. BUT without refreshing...
0
by: rnaimon | last post by:
I found the following information on this site, but I am unable to get it to work. All it brings back is the xml page with no data. I am running a Windows 2003 Server with Exchange 2003 Server. ...
0
by: news.orange.nl | last post by:
Hi! I'm looking for resources on howto get public folder data from MS exchange. More specifically, i'd like to show a certain catagory of contacts on our website, thus preventing double work. ...
2
by: Macias | last post by:
Hi, Please tell me how I can exchange data between two forms. My main form is Form1, and I display a new form something like this: private void Settings_Click(object sender, EventArgs e) { using...
0
by: =?Utf-8?B?YXVsZGg=?= | last post by:
hello i need some assistance on how to start on this project. i was given a very large wish list to collect from and Exchange environment. Exchange name LDAP server name Exchange IP address...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.