函数 —— popen() fscanf() sprintf() 执行shell命令并获取结果
對fopen()函數的理解如下:
| 表頭文件 | #include<stdio.h> | ||
| 定義函數 | FILE * popen( const char * command,const char * type); | ||
| 函數說明 | popen()會調用fork()產生子進程,然后從子進程中調用/bin/sh -c來執行參數command的指令。參數type可使用“r”代表讀取,“w”代表寫入。依照此type值,popen()會建立管道連到子進程的標準輸出設備或標準輸入設備,然后返回一個文件指針。隨后進程便可利用此文件指針來讀取子進程的輸出設備或是寫入到子進程的標準輸入設備中。此外,所有使用文件指針(FILE*)操作的函數也都可以使用,除了fclose()以外。
| ||
| 返回值 | 若成功則返回文件指針,否則返回NULL,錯誤原因存于errno中。 | ||
| 錯誤代碼 | EINVAL參數type不合法。 | ||
| 注意事項 | 在編寫具SUID/SGID權限的程序時請盡量避免使用popen(),popen()會繼承環境變量,通過環境變量可能會造成系統安全的問題。 | ||
| 范例 |
| ||
| 執行 | root :x:0 0: root: /root: /bin/bash |
對sprintf()函數的理解如下:
| 表頭文件 | #include<stdio.h> | ||
| 定義函數 | int? ?sprintf( char *string,char * farmat?[,argument,...]); | ||
| 函數說明 | sprintf指的是字符串格式化命令,主要功能是把格式化的數據寫入某個字符串中。sprintf 是個變參函數。 | ||
| 返回值 | 返回寫入string 的字符數,出錯則返回-1. 如果 string或 format 是空指針,且不出錯而繼續,函數將返回-1,并且 errno 會被設置為 EINVAL。 sprintf 返回以format為格式argument為內容組成的結果被寫入buffer 的字節數,結束字符‘\0’不計入內。即,如果“Hello”被寫入空間足夠大的buffer后,函數sprintf 返回5.? 同時buffer的內容將被改變。 | ||
| 錯誤代碼 | EINVAL參數type不合法。 | ||
| 注意事項 | 使用sprintf 對于寫入buffer的字符數是沒有限制的,這就存在了buffer溢出的可能性。 | ||
| 范例 |
| ||
| 執行 | I love CSDN 11 | ||
| 如何在sprintf中打印“雙引號?%百分號? | #include <stdio.h>
#include <time.h>
int main()
{char s[80];sprintf(s,"% %\""); //轉義字符方法printf("%s\n",s); //%"sprintf(s,"%c %c",'%','"');//字符常量方法printf("%s\n",s); //% "return 0;
}
? |
對fscanf()函數的理解如下:
| 表頭文件 | #include<stdio.h> |
| 定義函數 | int?fscanf(FILE*stream,constchar*format,[argument...]); |
| 函數說明 | 從一個流中執行格式化輸入,fscanf遇到空格和換行時結束,注意空格時也結束。這與fgets有區別,fgets遇到空格不結束。 |
| 返回值 | 整型,成功返回讀入的參數的個數,失敗返回EOF(-1) |
| ? | ? |
| 注意事項 | fscanf遇到空格和換行時結束,注意空格時也結束 |
| 范例 | #include <stdio.h> #include <stdlib.h>int main() {char str1[10], str2[10], str3[10];int year;FILE * fp; int count = 0;fp = fopen ("file.txt", "w+");fputs("We are in 2014", fp);rewind(fp);count = fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);printf("Read String1 |%s|\n", str1 );printf("Read String2 |%s|\n", str2 );printf("Read String3 |%s|\n", str3 );printf("Read Integer |%d|\n", year );printf("Count |%d|\n", count ); fclose(fp);return(0); } <stdio.h> #include <stdlib.h>int main() {char str1[10], str2[10], str3[10];int year;FILE * fp; int count = 0;fp = fopen ("file.txt", "w+");fputs("We are in 2014", fp);rewind(fp);count = fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);printf("Read String1 |%s|\n", str1 );printf("Read String2 |%s|\n", str2 );printf("Read String3 |%s|\n", str3 );printf("Read Integer |%d|\n", year );printf("Count |%d|\n", count ); fclose(fp);return(0); } |
| 執行 | Read String1 |We| Read String2 |are| Read String3 |in| Read Integer |2014| Count |4| |
?簡單的使用--1:
? ? ? ?問題描述:opoen函數執行,shell命令中的cat命令,并用fscanf函數獲取查找到的值
?
int main() {FILE *fp;char cmd[255] = {0};char tmp[255] = {0};sprintf(cmd,"cat /etc/passwd");printf("cmd == %s\n",cmd); //cmd == cat /etc/passwdfp = popen(cmd,"r");fscanf(fp,"%s",tmp);printf("tmp=%s strlen(tmp)=%d\n",tmp,strlen(tmp)); //tmp=root:x:0:0:root:/root:/bin/bash strlen(tmp)=31 pclose(fp); }簡單的使用--2:
? ? 問題描述:查找文件中的某個字段,存在則重寫,不存在則加入最后一行。
#include<stdio.h> #include<stdio.h> #include<stdlib.h> #define TEST_CONFIG "test.conf"int main() {FILE *fp;char *cmd = (char*)malloc(sizeof(char)*128);int input = 0;int line = 0;printf("Please input you want to find number:");scanf("%d",&input);//sprintf(cmd,"cat %s | awk '{print $1}' | grep -v '^\\$' | grep -v '^#' | grep -n '^%d$'",TEST_CONFIG,input);sprintf(cmd,"cat %s | awk '{print $1}'| grep -n '^%d$'",TEST_CONFIG,input); // 需要\字符使用\\來代替 '^str$'grep中:^匹配以str開頭,$匹配以str結尾(嚴格匹配)printf("%s\n",cmd);fp = popen(cmd,"r");fscanf(fp,"%d",&line);printf("line == %d\n",line);pclose(fp);if(line != 0) //exist , rewrite ==》 input+1{printf("exit!\n");sprintf(cmd,"sed -i '%dc %d' %s",line,input+1,TEST_CONFIG);}else{printf("bb\n");sprintf(cmd,"sed -i '$a%d' %s",input,TEST_CONFIG);printf("%s\n",cmd);}fp = popen(cmd,"r");pclose(fp);free(cmd);return 0; } /*結果如下所示:***test.conf $11 #44 55***./a.out Please input you want to find number:55 cat test.conf | awk '{print $1}'| grep -n '^55$' line == 3 exit!***result $11 #44 56***./a.out cat test.conf | awk '{print $1}'| grep -n '^77$' line == 0 bb sed -i '$a77' test.conf***result $11 #44 56 77 */簡單的使用--2:
? ? 問題描述:替換文件中的某一列的值
? ??asdfgh popen.txt? ? ? ? ? ? ?《--【?文件該之前】? ? ? ? ? ? ? ? ? ? ? ?asdfgh popen
? ? qwerty popen.c? ? ? ? ? ? ? ? ?【文件該之后】--》? ? ? ? ? ? ? ? ? ? ? ?qwerty popen
?
awk的其他的使用請看【Linux命令之 —— grep \ls \ ll \ sed \ bg fg \ ipset \ wc \ ifconfig \ awk】
參考鏈接:https://www.cnblogs.com/52php/p/5722238.html
總結
以上是生活随笔為你收集整理的函数 —— popen() fscanf() sprintf() 执行shell命令并获取结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openLDAP的编译安装以及配置
- 下一篇: Openldap配置TLS加密传输(完整