system()的替代 [英] Alternative to system()

查看:181
本文介绍了system()的替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近进入了c ++编程,我得到了很多经验。我过去的几个程序已经使用system()命令,我已经阅读应该是一个很糟糕的主意。首先,为什么这是一个坏主意?
我在Linux上使用它来清除屏幕[例如:system(clear)]和启动我的程序的其他部分,如返回到菜单[ex:system )]。是否可以使用它的东西,我上面显示的(这是主要的东西反正),还是有更好的解决方案?我只是作为一个爱好,但我仍然真的不喜欢做肮脏的程序。提前感谢!

I recently got into c++ programming, and I have gotten a lot of experience with it. My past few programs have been using the system() command, which I have read is supposed to be a really bad idea. First of all, why is it such a bad idea? I am using it on Linux for things like clear the screen [ex: system("clear") ] and to launch other parts of my program, such as returning to a menu [ex: system("./menu") ]. Is it okay to use it for things as I showed above (which are the main things anyway), or is there a better solution? I just program as a hobby, but I still really don't like making crappy programs. Thanks in advance!!

推荐答案

系统的问题可以分类作为三个或四个不同的问题:

The problems with system can be categorised as three or four different issues:


  1. 资源使用 - 系统使用相当多的系统资源,包括内存,执行时间和文件系统I / O需要创建一个新进程并在其中执行一个shell。

  1. Resource usage - system uses a fair bit of system resources, both memory, execution time and filesystem I/O is needed to create a new process and execute a shell inside it.

外部程序的可移植性( system(clear); 因为cls命令用于清除屏幕 - 或者至少不会清除屏幕,如果你真的不幸运,有人写了一个名为clear的清除程序,如果您是sysadmin用户,则清除客户数据库。

Portability of external programs (system("clear"); won't work on Windows, since the "cls" command is used to clear the screen - or at least won't clear the screen, if you are really unlucky, someone wrote a cleanup program called "clear" that erases the customer database if you are a sysadmin user. It gets even more fun if you need to pass arguments to the program, and different environments use different set(s) of options and arguments.

安全性如果您需要将参数传递给程序,并且不同的环境使用不同的选项和参数集,你从你的应用程序调用 system(clear); ,有人用一些不同的代码取代了本地清除,也擦除一些文件,或多或少的邪恶,这将是一件坏事,因为你启动一个外部程序,你需要非常具体的程序的路径,然后遇到可移植性问题。

Security. If you call system("clear"); from your application, and someone replaced the local "clear" with some different code that also erases some files, or something more or less evil, that would be a bad thing. Since you start an external program, you need to either be very specific to the path of that program, and then you run into portability problems.

解决方案真的取决于你想要达到的目的。

What the solution is really depends on what you are trying to achieve.

,使用 system 或其中一个紧密相关的函数,如 popen()将是正确的。在Linux和其他Unix版本上,使用 fork()后跟 exec()可能是正确的 - 至少它减少了资源开销情况。

And for a SMALL number of cases, using system or one of the closely related functions, such as popen() would be the right thing. On Linux and other flavours of Unix, using fork() followed by exec() may be the right thing - at least it reduces the resource overhead case.

system(clear); 的情况下,可以替换为输出短序列的字符 - 例如\033 [0J,它适用于任何ANSI / VT100兼容的终端/控制台窗口 - 这几乎覆盖了你会发现这些天,除非你真的去寻找模糊和不寻常的系统。 [除Windows之外,您需要在控制台API中调用clearscreen函数。]

In the case of system("clear");, it can can be replaced with outputting a short sequence of characters - such as "\033[0J", which works on any ANSI/VT100 compatible terminal/console window - which pretty much covers anything you'd ever find these days unless you REALLY go searching for obscure and unusual systems. [Except Windows, where you need to call the "clearscreen" function in the console API].

一般来说,如果需要可移植性, system()通常不是一个好的解决方案,并且使用在多个平台上可用的库[或创建自己]是更好的选择。

In general, if portability is a need, system() is usually not a good solution, and using a library that is available on multiple platforms [or creating such yourself] is a better choice.

从资源和可移植性的角度来看,调用 system(./ menu); 的例子听起来不是正确的,启动一个新的菜单程序,您可能已经运行了一个程序,除非您的菜单程序成功选择后退出。如果你继续开始越来越多的进程,它将EVENTUALLY填充整个系统,即使在一个具有大量内存的现代系统需要一段时间。

Both from a resource and portability standpoint, your example of calling system("./menu"); doesn't sound like the right thing either, since that would start a NEW menu program, which you probably already did run one - unless your menu program exits after successful choice. If you keep starting more and more processes, it will EVENTUALLY fill the entire system, even if it takes a while on a modern system with lots of memory.

这里,我的解决方案是创建一个包含所有功能的更大的程序,并且主菜单只需调用程序中的函数。通常可以使用某种类型的层次结构创建多层菜单,其中一个菜单选择导致另一个菜单或做某事 - 类层次结构或某种类型的函数指针表可能是这里的正确解决方案,但是,这是一个相当广泛的主题,肯定没有一个答案是正确的。

Here, my solution would be to build a larger program that contains all of the functionality, and have the main menu simply call the functions within the program. It is often possible to create multiple layer of menus using a hierarchical structure of some sort, where one menu choice either leads to another menu or "doing something" - a class hierarchy or some sort of table of function pointers MAY be the right solution here, but again, it's a pretty broad subject, and there is definitely not ONE answer that is right.

这篇关于system()的替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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