日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

函数 —— popen() fscanf() sprintf() 执行shell命令并获取结果

發布時間:2025/10/17 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 函数 —— 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()以外。

  • 如果 type 為 r,那么調用進程讀進?command 的標準輸出。
  • 如果 type 為 w,那么調用進程寫到 command 的標準輸入。

返回值

若成功則返回文件指針,否則返回NULL,錯誤原因存于errno中。

錯誤代碼

EINVAL參數type不合法。

注意事項

在編寫具SUID/SGID權限的程序時請盡量避免使用popen(),popen()會繼承環境變量,通過環境變量可能會造成系統安全的問題。

范例

1

2

3

4

5

6

7

8

9

10

11

#include<stdio.h>

?

main()

{

????FILE?*fp;

????char?buffer[80];

????fp = popen("cat /etc/passwd",?"r");

????fgets(buffer,?sizeof(buffer), fp);

????printf("%s", buffer);

????pclose(fp);

}

執行

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溢出的可能性。

范例

1

2

3

4

5

6

7

8

9

10

11

#include<stdio.h>

?

int main()

{

? ? char buf[100] = {0};

????char?*who = "I";

? ? char *whom = "CSDN";

?????int count = 0;

?

? ? count = sprintf(buf,"%s love %s",who,whom);

? ? printf("%s\n",buf);

????printf("%d\n", count);

????pclose(fp);

}

執行

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
?

#include <stdio.h> int main() {FILE *fp;char cmd[255] = {0};char line[255] = {0};char newline[255] = {0};int i = 1, j = 1;sprintf(cmd,"cat popen.txt");printf("%s\n",cmd);fp = popen(cmd,"r");if(NULL == fp){return 1;}while(NULL != fgets(line,sizeof(line),fp)) {printf("line[%d] = %s",i,line); i++;}pclose(fp);//替換文件中的第二列 全部為popen, [[ popen引號需要轉義]] [[mv時候加-f取消 提示輸入y/n]] //[[并且要加cat命令 否則后需的fgets沒有值]]sprintf(cmd,"awk '{print $1,\"popen\"}' popen.txt > temp_1 && mv -f temp_1 popen.txt && cat popen.txt");printf("%s\n",cmd);fp = popen(cmd,"r");if(NULL == fp){ printf("asda\n");return 1;}while(NULL != fgets(line,sizeof(line),fp)) {printf("line[%d] = %s",j,line); j++;}pclose(fp);return 0; } //結果顯示如下::: line[1] = asdfgh popen.txt line[2] = qwerty popen.c awk '{print $1,"popen"}' popen.txt > temp_1 && mv -f temp_1 popen.txt && cat popen.txt line[1] = asdfgh popen line[2] = 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命令并获取结果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。