写文件 —— 将内容按照指定格式写入配置文件(fprintf()函数-》》本机的监听地址列表中port值)
例如 —— 文件中的配置內容格式如下:
listen的文件的內容為
listen? 33;
listen?? 24;
參數說明
編輯
int fprintf (FILE* stream, const char*format, [argument])
FILE*stream:文件指針
const char* format:輸出格式
[argument]:附加參數列表
函數說明
編輯
fprintf( )會根據參數format 字符串來轉換并格式化數據, 然后將結果輸出到參數stream 指定的文件中, 直到出現字符串結束('\0')為止。
運行結果顯示:
[root@J01051386 mapproxy]# cat listenlisten 11223;listen 25;listen 1224;listen 8080;代碼部分:
???
#include <stdio.h>#include <string.h>int main(){int bl = 0;char *pcpos = NULL;FILE *fp = NULL;FILE *pf = NULL;char szListen[2048] = {0};pf = popen("sudo netstat -nl | grep tcp | awk '{print $4}' | awk '{split($0,a,\":\");print a[2]a[4]}'", "r");if(NULL == pf){return 1;}if((fp = fopen("/usr/local/nginx/conf/mapproxy/listen", "w")) == NULL){printf("Cannot open output file.\n");return 1;}while(NULL != fgets(szListen, sizeof(szListen), pf)){ szListen[strlen(szListen) - 1] = '\0';fprintf(fp, "listen %s;\n", szListen);}fclose(fp);fclose(pf);return 0;}相關問題:
[root@localhost netsec+]# sudo netstat -nl | grep tcp??? (匹配tcp與tcp6的數據)
tcp??????? 0????? 0 0.0.0.0:11223?????????? 0.0.0.0:*?????????????? LISTEN??? ?
tcp??????? 0????? 0 0.0.0.0:25????????????? 0.0.0.0:*?????????????? LISTEN??? ?
tcp6?????? 0????? 0 :::1224???????????????? :::*??????????????????? LISTEN??? ?
tcp6?????? 0????? 0 :::8080???????????????? :::*??????????????????? LISTEN
[root@localhost netsec+]# sudo netstat -nl | grep -w tcp??? (匹配tcp數據)
tcp??????? 0????? 0 0.0.0.0:11223?????????? 0.0.0.0:*?????????????? LISTEN??? ?
tcp??????? 0????? 0 0.0.0.0:25????????????? 0.0.0.0:*?????????????? LISTEN??? ?
tcp6?????? 0????? 0 :::1224???????????????? :::*??????????????????? LISTEN??? ?
tcp6?????? 0????? 0 :::8080???????????????? :::*??????????????????? LISTEN
---------------------
作者:杰兒__er
來源:CSDN
原文:https://blog.csdn.net/weixin_42167759/article/details/84337852
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
總結
以上是生活随笔為你收集整理的写文件 —— 将内容按照指定格式写入配置文件(fprintf()函数-》》本机的监听地址列表中port值)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 判断 —— if...else 语句
- 下一篇: 写文件 —— 将内容按照指定格式写入配置