c語言execlp
Ⅰ c語言中 使用execl函數創建一個文件
execl("/bin/touch","touch","./abc",NULL);在當前目錄下創建空文件abc
Ⅱ 如何在C語言中執行shell命令
可以通過system函數,調用shell命令。
1 函數原型:
int system(const char *cmd);
2 功能:
調用cmd內容的系統命令,即shell命令。
3 頭文件:
stdlib.h
4 舉例:
system("ls");
列印當前工作目錄下的文件。
Ⅲ C語言exec...()函數
函數名: exec...
功 能: 裝入並運行其它程序的函數
用 法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL);
int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,
char *envp[]);
int execlp(char *pathname, char *arg0, arg1, .., NULL);
int execple(char *pathname, char *arg0, arg1, ..., NULL,
char *envp[]);
int execv(char *pathname, char *argv[]);
int execve(char *pathname, char *argv[], char *envp[]);
int execvp(char *pathname, char *argv[]);
int execvpe(char *pathname, char *argv[], char *envp[]);
程序例:/* execv example */
#include <process.h>
#include <stdio.h>
#include <errno.h>void main(int argc, char *argv[])
{
int i; printf("Command line arguments:\n");
for (i=0; i<argc; i++)
printf("[%2d] : %s\n", i, argv[i]); printf("About to exec child with arg1 arg2 ...\n");
execv("CHILD.EXE", argv); perror("exec error"); exit(1);
}
Ⅳ c語言如何 調用外部程序
調用外部程序介面
方法1.
Process p=Runtime.getRuntime.exec("cmd")(最常用)
方法2.
Process p=new ProcessBuilder(cmd).start()
但是一般方法一比較常用, 下面我們介紹下方法一中關於抽象Process類的常用函數
//向對應程序中輸入數據
();
//獲得對應程序的輸出流(沒寫錯)
();
//獲得程序的錯誤提示
();
//等待程序執行完成,返回0正常,返回非0失敗
abstractpublicintwaitFor()throwsInterruptedException;
//獲得程序退出值,0正常退出,非0則異常
abstractpublicintexitValue();
//銷毀進程
abstractpublicvoiddestroy();
其中前3個函數用的最多
Ⅳ C語言函數的進程函數
所在函數庫為stdlib.h、process.h
void abort() 此函數通過調用具有出口代碼3的_exit寫一個終止信息於
stderr,並異常終止程序 無返回值
int exec…裝入和運行其它程序
int execl( char *pathname,char *arg0,char *arg1,…,char *argn,NULL)
int execle( char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int execlp( char *pathname,char *arg0,char *arg1,…,NULL)
int execlpe(char *pathname,char *arg0,char *arg1,…,NULL,char *envp[])
int execv( char *pathname,char *argv[])
int execve( char *pathname,char *argv[],char *envp[])
int execvp( char *pathname,char *argv[])
int execvpe(char *pathname,char *argv[],char *envp[])
exec函數族裝入並運行程序pathname,並將參數
arg0(arg1,arg2,argv[],envp[])傳遞給子程序,出錯返回-1
在exec函數族中,後綴l、v、p、e添加到exec後,
所指定的函數將具有某種操作能力
有後綴 p時,函數可以利用DOS的PATH變數查找子程序文件
l時,函數中被傳遞的參數個數固定
v時,函數中被傳遞的參數個數不固定
e時,函數傳遞指定參數envp,允許改變子進程的環境,
無後綴e時,子進程使用當前程序的環境
void _exit(int status)終止當前程序,但不清理現場
void exit(int status) 終止當前程序,關閉所有文件,寫緩沖區的輸出(等待輸出),
並調用任何寄存器的出口函數,無返回值
int spawn…運行子程序
int spawnl( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnle( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnlp( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnlpe(int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnv( int mode,char *pathname,char *argv[])
int spawnve( int mode,char *pathname,char *argv[],char *envp[])
int spawnvp( int mode,char *pathname,char *argv[])
int spawnvpe(int mode,char *pathname,char *argv[],char *envp[])
spawn函數族在mode模式下運行子程序pathname,並將參數
arg0(arg1,arg2,argv[],envp[])傳遞給子程序.出錯返回-1
mode為運行模式
mode為 P_WAIT 表示在子程序運行完後返回本程序
P_NOWAIT 表示在子程序運行時同時運行本程序(不可用)
P_OVERLAY表示在本程序退出後運行子程序
在spawn函數族中,後綴l、v、p、e添加到spawn後,
所指定的函數將具有某種操作能力
有後綴 p時, 函數利用DOS的PATH查找子程序文件
l時, 函數傳遞的參數個數固定.
v時, 函數傳遞的參數個數不固定.
e時, 指定參數envp可以傳遞給子程序,允許改變子程序運行環境.
當無後綴e時,子程序使用本程序的環境.
int system(char *command) 將MSDOS命令command傳遞給DOS執行
轉換子程序,函數庫為math.h、stdlib.h、ctype.h、float.h
char *ecvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字元串並返回該字元串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字元串並返回該字元串
char *gcvt(double value,int ndigit,char *buf)
將數value轉換成字元串並存於buf中,並返回buf的指針
char *ultoa(unsigned long value,char *string,int radix)
將無符號整型數value轉換成字元串並返回該字元串,radix為轉換時所用基數
char *ltoa(long value,char *string,int radix)
將長整型數value轉換成字元串並返回該字元串,radix為轉換時所用基數
char *itoa(int value,char *string,int radix)
將整數value轉換成字元串存入string,radix為轉換時所用基數
double atof(char *nptr) 將字元串nptr轉換成雙精度數,並返回這個數,錯誤返回0
int atoi(char *nptr) 將字元串nptr轉換成整型數, 並返回這個數,錯誤返回0
long atol(char *nptr) 將字元串nptr轉換成長整型數,並返回這個數,錯誤返回0
double strtod(char *str,char **endptr)將字元串str轉換成雙精度數,並返回這個數,
long strtol(char *str,char **endptr,int base)將字元串str轉換成長整型數,
並返回這個數,
int toascii(int c) 返回c相應的ASCII
int tolower(int ch) 若ch是大寫字母('A'-'Z')返回相應的小寫字母('a'-'z')
int _tolower(int ch) 返回ch相應的小寫字母('a'-'z')
int toupper(int ch) 若ch是小寫字母('a'-'z')返回相應的大寫字母('A'-'Z')
int _toupper(int ch) 返回ch相應的大寫字母('A'-'Z')
Ⅵ C語言execl()函數 執行文件函數
C語言execl()函數:執行文件函數
相關函數:fork, execle, execlp, execv, execve, execvp
頭文件:#include <unistd.h>
定義函數:int execl(const char * path, const char * arg, ...);
函數說明:execl()用來執行參數path 字元串所代表的文件路徑, 接下來的參數代表執行該文件時傳遞過去的argv(0), argv[1], ..., 最後一個參數必須用空指針(NULL)作結束.
返回值:如果執行成功則函數不會返回, 執行失敗則直接返回-1, 失敗原因存於errno 中.
範例
#include <unistd.h>
main()
{
execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *)0);
}
執行:
/*執行/bin/ls -al /etc/passwd */
-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
Ⅶ c語言常用函數有哪些主要掌握的要點是什麼
標准頭文件包括:
<asset.h> <ctype.h> <errno.h> <float.h>
<limits.h> <locale.h> <math.h> <setjmp.h>
<signal.h> <stdarg.h> <stddef.h> <stdlib.h>
<stdio.h> <string.h> <time.h>
一、標準定義(<stddef.h>)
文件<stddef.h>里包含了標准庫的一些常用定義,無論我們包含哪個標准頭文件,<stddef.h>都會被自動包含進來。
這個文件里定義:
l 類型size_t (sizeof運算符的結果類型,是某個無符號整型);
l 類型ptrdiff_t(兩個指針相減運算的結果類型,是某個有符號整型);
l 類型wchar_t (寬字元類型,是一個整型,其中足以存放本系統所支持的所有本地環境中的字元集的所有編碼值。這里還保證空字元的編碼值為0);
l 符號常量NULL (空指針值);
l 宏offsetor (這是一個帶參數的宏,第一個參數應是一個結構類型,第二個參數應是結構成員名。
offsetor(s,m)求出成員m在結構類型t的變數里的偏移量)。
註:其中有些定義也出現在其他頭文件里(如NULL)。
二、錯誤信息(<errno.h>)
<errno.h>定義了一個int類型的表達式errno,可以看作一個變數,其初始值為0,一些標准庫函數執行中出錯時將它設為非0值,但任何標准庫函數都設置它為0。
<errno.h>里還定義了兩個宏EDOM和ERANGE,都是非0的整數值。數學函數執行中遇到參數錯誤,就會將errno置為EDOM,如出現值域錯誤就會將errno置為ERANGE。
三、輸入輸出函數(<stdio.h>)
文件打開和關閉:
FILE *fopen(const char *filename, const char *mode);
int fclose(FILE * stream);
字元輸入輸出:
int fgetc(FILE *fp);
int fputc(int c, FILE *fp);
getc和putc與這兩個函數類似,但通過宏定義實現。通常有下面定義:
#define getchar() getc(stdin)
#define putchar(c) putc(c, stdout)
int ungetc(int c, FILE* stream);//把字元 c 退迴流 stream
格式化輸入輸出:
int scanf(const char *format, ...);
int printf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sscanf(char *s, const char *format, ...);
int sprintf(char *s, const char *format, ...);
行式輸入輸出:
char *fgets(char *buffer, int n, FILE *stream);
int fputs(const char *buffer, FILE *stream);
char *gets(char *s);
int puts(const char *s);
直接輸入輸出:
size_t fread(void *pointer, size_t size, size_t num, FILE *stream);
size_t fwrite(const void *pointer, size_t size, size_t num, FILE *stream);