如何从 ac 程序内部调用 awk 或 sed?
如何在 ac 程序中调用 awk 或 sed? 我知道我可以使用 exec(),但我不想处理 fork() 和所有其他讨厌的事情。
How can I call awk or sed inside a c program? I know I could use exec(), but I don't want to deal with fork() and all that other nastiness.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
popen
有用吗? 它生成进程,然后您使用FILE*
句柄进行读/写Would
popen
work? It spawns the process, then you read/write with aFILE*
handle那么你的选择是
system()
,或者使用一些为你包装进程生成的库。 如果您想要对错误、管道等进行精细控制,建议您使用后者,或者您想要避免的硬核方式。Then your choice is
system()
, or using some library that wraps process spawning for you. The latter, or the hard-core way you wanted to avoid, is recommended if you want fine control over errors, pipes, and so on.system() 很简单。
但如果可以的话,你应该尽量不要这样做。 脚本在位于事物之上而不是在其之下时效果最佳。 如果您使用 UNIX,通常最好将工作分解并编写一个顶级脚本来调用所有部分。
我记得看到一个程序员在他的 C 代码中添加了大量的系统调用,以避免学习 Bourne shell。 他认为这是一个聪明而快速的方法,但当它失败时,它会严重失败。 他浪费了大量时间来调试混乱的情况。 如果只学习一些简单的 shell 命令会更快...
Paul。
system() is easy enough.
But you should try not to do this if you can. Scripts work best when they are on top of things, not underneath. If you're in UNIX, it's often way better to break up the work and write a top level script to call all of the pieces.
I remember watching a programmer add a huge number of system calls into his C code in order to avoid having to learn the Bourne shell. He figured it was a clever and quick way to get it going, however when it failed, it failed badly. He wasted huge amounts of time debugging the mess. It would have been way faster to just learn a few simple shell commands...
Paul.
libc
具有函数system
和popen
,它们的工作方式有点像这样:(除了更多的错误检查和其他内容)
如果你想更好地控制参数处理(而不是通过
sh
),或者您想要控制{stdin, stdout, stderr}
中的多个,您必须自己编写或查找图书馆。 但标准库涵盖了大多数用例。libc
has functionssystem
andpopen
, which work kinda like this:(except with more error checking and stuff)
If you want finer control over argument handling (instead of going through
sh
), or you want control over more than one of{stdin, stdout, stderr}
, you'll have to write it yourself or find a library. But the standard library covers most use cases.您可以通过system()调用来完成此Thread 就是一个很好的例子
You can do it via the system() call This Thread is a good example