473,395 Members | 1,676 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,395 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 2924
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.