llCommand.o
rm *.o
转到“release”目录下,运行TestShellCommand(有两种运行方式:TestShellCommand和./TestShellCommand),结果如下:
zhou@linux:~/zhouzx/TestShellCommand/release> TestShellCommand
Input the index of source file: 123456
The delete command is: rm -rf TestFile/123456.*
zhou@linux:~/zhouzx/TestShellCommand/release> TestShellCommand
Input the index of source file: 123456.r
The delete command is: rm -rf TestFile/123456.r
zhou@linux:~/zhouzx/TestShellCommand/release> ./TestShellCommand
Input the index of source file: 123456
The delete command is: rm -rf TestFile/123456.*
zhou@linux:~/zhouzx/TestShellCommand/release> ./TestShellCommand
Input the index of source file: 123456.r
The delete command is: rm -rf TestFile/123456.r
上面执行了四次TestShellCommand,123456.r文件都被成功删除掉了。为了验证程序的正确性,可以多用一些文件来进行测试。
六、总结
本文对C语言程序中执行shell命令的方法作了详细的介绍,并用实际的程序代码来演示了其执行结果。
在C语言程序执行shell命令的过程中,我们要注意以下问题:
(1) 一定要将shell命令配置正确,并在其执行前后打印详细的日志,方便对程序进行调试和查错。
(2) 在进行删除操作的之前,还要仔细检查shell命令的正确性,确保程序不会误删除。
在实际的软件开发项目中,C语言程序运行于Linux之下已是十分常见。本文介绍了C语言程序中执行shell命令的过程,为相关软件开发工作的顺利开展提供了有益的参考。
(欢迎访问南邮BBS:http://bbs.njupt.edu.cn/)
(欢迎访问重邮BBS:http://bbs.cqupt.edu.cn/nForum/index)
(本系列文章每周更新两篇,敬请期待!本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,微信号:245924426,欢迎关注!)