写文件 —— 将内容按照指定格式写入配置文件(fprintf()函数-》》本机的监听地址列表中port值)
例如 —— 文件中的配置內(nèi)容格式如下:
listen的文件的內(nèi)容為
listen? 33;
listen?? 24;
參數(shù)說明
編輯
int fprintf (FILE* stream, const char*format, [argument])
FILE*stream:文件指針
const char* format:輸出格式
[argument]:附加參數(shù)列表
函數(shù)說明
編輯
fprintf( )會(huì)根據(jù)參數(shù)format 字符串來轉(zhuǎn)換并格式化數(shù)據(jù), 然后將結(jié)果輸出到參數(shù)stream 指定的文件中, 直到出現(xiàn)字符串結(jié)束('\0')為止。
運(yùn)行結(jié)果顯示:
[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;}相關(guān)問題:
[root@localhost netsec+]# sudo netstat -nl | grep tcp??? (匹配tcp與tcp6的數(shù)據(jù))
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數(shù)據(jù))
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
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
總結(jié)
以上是生活随笔為你收集整理的写文件 —— 将内容按照指定格式写入配置文件(fprintf()函数-》》本机的监听地址列表中port值)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 判断 —— if...else 语句
- 下一篇: 写文件 —— 将内容按照指定格式写入配置