python - Problem using pipe with c++ and python in a loop
问题描述
I want to make a pipe between C++ and Python. The goal is to compute some numbers in C++ and as I'm computing them I want to send them to Python in real time.
So far i can send just an int from C++ to Python using this.
stdout.cpp:
#include <iostream>
int main () {
int x = 59;
cout << x;
return 0;
}
stdin.py:
import sys
import pickle
import fileinput
file2write=open("filename",'w')
stdout = sys.stdin.readlines()
file2write.write(str(stdout))
file2write.close()
So far it runs ok and I cand check "filename" and see that python is writing 59 there. But I can't do this in a loop. What I want is something that could process something like
stdout.cpp:
#include <iostream>
int main () {
int x = 59;
while(1){
cout << x << "\n";
}
return 0;
}
And be able to add each line to the file in python as the program is running. The goal is to do this in an infinite loop and write to the file as the program is running. But when I do something like this the file "filename" ends empty.
What should I do?
解决方案
推荐阅读
- android - 如何在 Android 布局 xml 文件中获取小部件的完整类名?
- javascript - 在javascript中将JSON对象转换为数组数组
- firebase - 取消订阅后,Firebase 主题仍显示在控制台中
- c++ - 将有符号和无符号字符都写入 write()
- javascript - 将查询结果从 Google Cloud Spanner 导出到 Google 表格
- c++ - 在 CLinkCtrl 上按下空格键后调用 TrackPopupMenu() 会导致发出警告消息。为什么?
- python - Python自定义模块未找到
- java - 在我的 eclispe 项目中只为一个班级制作一个胖罐子
- python - 带有 numpy 的 SVD - 结果的解释
- typescript - VSCode、Electron、TypeScript、调试渲染器进程、设置跳过文件?