C ++将字符串传递到管道中以传递给gnuplot

C++ Pass a string into a pipe to gnuplot

我在将字符串从 C++ 传递给 gnuplot 时遇到了一个小问题,我可以很容易地传递整数,但是当我尝试一个字符串(用户在代码前面定义为 "title" 时):

fprintf(gnuplotPipe, "set title %s\n", title);

我收到错误:

error: cannot pass objects of non-trivially-copyable type ‘std::string {aka class std::basic_string<char>}’ through ‘...’

所以我尝试使用:

fprintf(gnuplotPipe, "set title %s\n", title.c_str());

并且代码实际编译了,但是当我 运行 它时,我从 Gnuplot 得到了错误:

line 0: undefined variable: h

其中 "h" 只是 "title" 定义的测试字符串。

有人知道如何通过这个吗?

提前致谢!

您必须将标题放在引号中:

fprintf(gnuplotPipe, "set title \"%s\"\n", title.c_str());