第1个回答 2011-07-24
注:这是CSDN上的RookieStar回答的
我以这个为测试为例:system( "abcde ");
显示如下:
'abcde ' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Press any key to continue
这个很能说明问题:在windows操作系统下,system()函数真正调用的是命令解释器文件cmd.exe,这个我想你应该知道吧,通过解释器将传入的字符串转成相应的内部/外部命令或可运行程序以及批处理文件,就能解决你关于pause(内部指令)、clrscr(外部命令)的问题了。不过这里还有个小问题,我的中文XP下cmd似乎也是英文版本的,所以pause显示英文,而console程序显示的是中文,我个人理解是console程序与操作系统间有密切关系(可能存在语言的转译或内部调用一个中文的cmd),所以console程序结果与cmd结果在内容上有语言的区别。至于内部指令的实现问题,还是让m$好好教你吧! :)
顺便附上msdn中关于system函数的说明,供你参考:
system, _wsystem
Execute a command.
int system( const char *command );
int _wsystem( const wchar_t *command );
Routine Required Header Compatibility
system <process.h> or <stdlib.h> ANSI, Win 95, Win NT
_wsystem <process.h> or <stdlib.h> or <wchar.h> Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
If command is NULL and the command interpreter is found, the function returns a nonzero value. If the command interpreter is not found, it returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values:
E2BIG
Argument list (which is system-dependent) is too big.
ENOENT
Command interpreter cannot be found.
ENOEXEC
Command-interpreter file has invalid format and is not executable.
ENOMEM
Not enough memory is available to execute command; or available memory has been corrupted; or invalid block exists, indicating that process making call was not allocated properly.
Parameter
command
Command to be executed
Remarks
The system function passes command to the command interpreter, which executes the string as an operating-system command. system refers to the COMSPEC and PATH environment variables that locate the command-interpreter file (the file named CMD.EXE in Windows NT). If command is NULL, the function simply checks to see whether the command interpreter exists.
You must explicitly flush (using fflush or _flushall) or close any stream before calling system.
_wsystem is a wide-character version of system; the command argument to _wsystem is a wide-character string. These functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tsystem system system _wsystem
Example
/* SYSTEM.C: This program uses
* system to TYPE its source file.
*/
#include <process.h>
void main( void )
{
system( "type system.c " );
}
Output
/* SYSTEM.C: This program uses
* system to TYPE its source file.
*/
#include <process.h>
void main( void )
{
system( "type system.c " );
}