Python 和 C++一体化。 Python 将字符串打印为多行
我正在尝试用 python 编写一个程序来运行 C++ 中的程序。它工作不正常,所以我尽我所能制作了最基本的版本。 C++ 程序仅从 stdin 获取一个字符串,然后将其打印出来。 Python 代码编写如下:
import popen2, string, StringIO
fin, fout = popen2.popen2("PyTest")
msg = ur"Hello, world!"
print msg
fout.write(msg)
print fin.readline()
输出看起来像这样:
Hello, world!
Hello,
我一直看到的问题是空格似乎会分隔字符串,即使它是字符串文字。我不太确定在这里做什么。有什么建议吗?
I'm trying to write a program in python to run a program in C++. It wasn't working right, so I made the most basic version of each I could.
The C++ program merely takes in a string from stdin, and then prints it out.
The Python code is written as follows:
import popen2, string, StringIO
fin, fout = popen2.popen2("PyTest")
msg = ur"Hello, world!"
print msg
fout.write(msg)
print fin.readline()
The output, however looks like this:
Hello, world!
Hello,
The problem I keep seeing is that spaces seem to break apart the string, even though it is a string literal. I'm not really sure what to do here. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C++ 中,
std::cin>> mystring
使用空格作为分隔符。如果您想一次吞噬一整行,请使用 std::getline 来代替。In C++,
std::cin >> mystring
uses spaces as separators. Usestd::getline
instead if you want to gobble up a whole line at a time.