系统调用不像在命令行中那样工作

发布于 2024-08-11 04:56:58 字数 851 浏览 2 评论 0原文

好吧,我有两个程序,一个程序使用另一个程序的可执行文件调用另一个程序。 我在 Ubuntu 终端上运行它

这是适当的文件夹结构

.../src/pgm1/pgm1 .../src/pgm0/pgm0

pgm1 和 pgm0 是可执行文件。

这就是我调用我查看的其他可执行文件

char cmd[1000]; string path = "/home/usr/src/"; // call pgm0 for each instance... sprintf( cmd, "../pgm0/pgm0 xRes 400 xRes 400 inFile tmp_output/%s.%04d.sc > tmp_output/%s.%04d.ppm", g_outFile.c_str(), ti, g_outFile.c_str(), ti); cout << cmd << endl; system (cmd); ....

并正确生成 cmd 的方式: ../pgm0/pgm0 yRes 400 xRes 400 inFile tmp_output/sph0.0000.sc > tmp_output/sph0.0000.ppm

因此,如果我从命令行运行此命令,它工作得很好。

如果我使用系统调用运行它,它会挂起并且无法解析输入文件 sph0.0000.sc 我尝试添加完整路径(因此路径变量向上)

但仍然没有运气。

任何想法为什么这可以从命令行工作,而不是从另一个可执行文件中的系统调用...

只是为了明确它可以从文件夹 pgm1 中的命令行工作。

谢谢

Ok I have two programs, and one calls another using executable from another.
I am running it on Ubuntu terminal

This is folder structure in place

.../src/pgm1/pgm1
.../src/pgm0/pgm0

pgm1 and pgm0 are executables.

This is how I call the other executable

char cmd[1000]; string path = "/home/usr/src/"; // call pgm0 for each instance... sprintf( cmd, "../pgm0/pgm0 xRes 400 xRes 400 inFile tmp_output/%s.%04d.sc > tmp_output/%s.%04d.ppm", g_outFile.c_str(), ti, g_outFile.c_str(), ti); cout << cmd << endl; system (cmd); ....

I looked over and the cmd is generated properly:
../pgm0/pgm0 yRes 400 xRes 400 inFile tmp_output/sph0.0000.sc > tmp_output/sph0.0000.ppm

So if I run this command from command line it works perfectly well.

If I run it using system call it hangs and fails to parse input file sph0.0000.sc
I tried adding full path (hence path variable up)

But still no luck.

Any ideas why would this work from command line and not from system call within another executable...

Just to make it clear it works from command line in folder pgm1.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-08-18 04:56:58

您正在使用 > 这对许多 shell 来说意味着一些东西,但我怀疑对 system 没有意义。试试这个:

snprintf( cmd, sizeof cmd,
    "/usr/bin/bash -c '../pgm0/pgm0 xRes 400 xRes 400"
    " inFile tmp_output/%s.%04d.sc > tmp_output/%s.%04d.ppm'",
    g_outFile.c_str(), ti, g_outFile.c_str(), ti);

让我们知道进展如何。

You are using > which means something to many shells, but I suspect not to system. Try this:

snprintf( cmd, sizeof cmd,
    "/usr/bin/bash -c '../pgm0/pgm0 xRes 400 xRes 400"
    " inFile tmp_output/%s.%04d.sc > tmp_output/%s.%04d.ppm'",
    g_outFile.c_str(), ti, g_outFile.c_str(), ti);

And let us know how that goes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文