c语言用命令行编译运行程序_使用C程序执行系统命令

c语言用命令行编译运行程序

Sometimes, we may need to execute Linux/Windows DOS commands through our C program. (Note: the code given below is compiled and executed on Linux GCC compiler, so here we are testing Linux commands only).

有时,我们可能需要通过C程序执行Linux / Windows DOS命令 。 (注意:下面给出的代码是在Linux GCC编译器上编译和执行的 ,因此这里我们仅测试Linux命令)。

In the C programming standard library, there is a function named system () which is used to execute Linux as well as DOS commands in the C program.

在C编程标准库中,有一个名为system()的函数,该函数用于执行Linux以及C程序中的DOS命令。

A command can be assigned directly to the function as an argument and command may also input from the user and then assigned to the function, function will send command to the operating system’s particular terminal like Linux terminal or DOS commands terminal to execute, and after the execution, you will get your output and program’s execution returns to the next statement written after the system() function.

可以将命令作为参数直接分配给函数,也可以从用户输入命令,然后将其分配给函数,函数会将命令发送到操作系统的特定终端(例如Linux终端或DOS命令终端)以执行,然后执行,您将获得输出,程序的执行返回到在system()函数之后编写的下一条语句。

C中的system()函数 (system() function in C)

system() is a library function, which is defined in the stdlib.h header file. It is used to execute the Linux commands/Windows DOS commands.

system()是一个库函数,该函数在stdlib.h头文件中定义。 它用于执行Linux命令/ Windows DOS命令。

Syntax:

句法:

system(char *command);

Example:

例:

    char *command = "ls";
    system(command);


在C程序中运行Linux命令的程序 (Program to run Linux commands within C program)

#include <stdio.h>
#include <stdlib.h>	//to use system()
#include <string.h> //to use strcpy()

int main()
{
	char *command;
	
	//executing ls command
	strcpy(command, "ls");
	printf("ls command...\n");
	system(command);
	
	//executing date command 
	strcpy(command, "date");
	printf("date command...\n");
	system(command);
	
	return 0;
}

Output

输出量

    Please run this program at your machine


翻译自: https://www.includehelp.com/c/executing-system-commands-using-c-program.aspx

c语言用命令行编译运行程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值