运行外部程序并读取其输出 [英] Run an external program and read its output

查看:76
本文介绍了运行外部程序并读取其输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在设计要在家里使用的小软件,但遇到了问题.

服务器端需要运行cd来更改当前目录,还需要运行ls来获取当前目录中的文件和目录列表.

关键是,我绝对不知道该怎么做.

有人可以帮我吗?

Hello,

I''m designing a little software to use at home and I ran into a problem.

The server side needs to run cd to change the current directory and also ls to get the list of files and directories in the current directory.

The point is, I have absolutely no idea how to do it.

Can anyone help me out please?

推荐答案

使用system()或类似方法运行cd命令不会更改调用进程的当前目录.您应该使用 chdir() [ opendir() [ readdir() [ boost ::文件系统 [
Running the cd command with system() or some similar method won''t change the current directory of the calling process. You should use the chdir()[^]/opendir()[^]/readdir()[^]/... functions or the boost::filesystem[^] library instead of external programs.


已经有一段时间没有在Linux上编程了,但是您应该能够使用C system()函数.参见 http://www.opengroup.org/onlinepubs/000095399/functions/system.html [ ^ ]

解析ls命令输出的最简单方法是将输出重定向到文本文件,然后对其进行解析.
Haven''t programmed in Linux in a while, but you should be able to use the C system() function. See http://www.opengroup.org/onlinepubs/000095399/functions/system.html[^]

The simplest way to parse the output of an ls command is to redirect output to a text file, and then parse that.


这篇关于运行外部程序并读取其输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆