c语言:读取文件中的指定内容

      项目中要求从wifi的状态文件中提取出rssi字段的值,还有一个前提条件:必须与hwaddr(手机的MAC地址)一一对应。

wifi状态文件sta_info如下:

# cat /proc/wlan0-vap0/sta_info
-- STA info table -- (active: 1)
 1: stat_info...
    state: 405
    AuthAlgrthm: 0
    ieee8021x_ctrlport: 0
    hwaddr: c06394e1468b
    bssrateset: 82848b960c1218243048606c
    aid: 1
    tx_bytes: 369918
    rx_bytes: 62005
    tx_pkts: 333
    rx_pkts: 901
    tx_fail: 0
    tx_avarage:    0
    rx_avarage:    0
    dz_queue_len: 0
    rssi: 62 (62 56)
    expired_time: 294
    sleep: yes
    current_tx_rate: MCS7 65
    current_rx_rate: MCS4 39
    rx_bw: 20M
    tx_bw: 20M
    hp_level: 0
    QoS Enable: 1
    APSD bitmap: 0x0
    tx_ra_bitmap: 0x000f0000
    Chip Vendor: Broadcom
    link_time: 4 min 44 sec 
    ht_cap: 0c181bff00000000000000000000000000000000000000000000
    11n MIMO ps: no limit
    Is_8K_AMSDU: 1
    amsdu_level: 7895
    diffAmpduSz: 0xffffffff
    aggre mthd: AMPDU
    ht beamformee: N
    ht beamformer: N
    Activate Tx beamforming : N
    ipmc_num: 1
    mcmac: 01005e0000fb
    bSleep: 01
    bDrop: 00
    FW bDrop: 00
    ht_ie: none
    wireless mode: BGN

     
我写了一个get_wifi_signal_intensity(const char* mac,char *intenstiy)函数,传入mac地址,输出信号强度值intenstiy,从而实现需求。get_wifi_signal_intensity函数实现如下:

#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"


static void get_wifi_signal_intensity(const char* mac,char *intenstiy)
{
    char dstmac[32] = {0};
    int i = 0;
    int found_mac = 0;
    int rssi = 0, dBm = 0;
    FILE * fp = NULL;
    char filename[64] = {0};
    char line[128] = {0};
	
    sprintf(filename,"/proc/wlan0-vap0/sta_info");   
	
    if ( mac && (fp = fopen(filename, "r")) )
    {
    	sscanf(mac,"%2s-%2s-%2s-%2s-%2s-%2s",dstmac,dstmac+2,dstmac+4,dstmac+6,dstmac+8,dstmac+10);
	for(i=0;i<strlen(dstmac);i++)
	{
	    dstmac[i] = tolower(dstmac[i]);
	}
		
        while(!feof(fp))
        {
            if (NULL == fgets (line, sizeof (line), fp))
            {
                /* Error or EOF */
                break;
            }

            if ( found_mac )
            {
                if( strstr(line,"rssi:") )
                {
                    sscanf(line, "%*s %d", &rssi);
                    break;
                }
                else
                {
                    continue;
                }
            }
            if( strstr(line,"hwaddr:") && strstr(line,dstmac) )
            {
                found_mac = 1;
            }
        }

        fclose(fp);
    }

    if (rssi <= 0)
        dBm = -100;
    else if (rssi >= 100)
        dBm = -50;
    else
        dBm = (rssi / 2) - 100;

    sprintf(intenstiy, "%d", dBm);
	
    printf("MAC is %s,dstmac is %s,rssi is %d,intenstiy is %d",mac,dstmac,rssi,intenstiy);
}

运行结果:

调用函数:get_wifi_signal_intensity("C0-63-94-E1-46-8B",intenstiy)
打印结果:MAC is C0-63-94-E1-46-8B,dstmac is c06394e1468b,rssi is 62,intenstiy is -69

函数实现的功能:

    输入一个MAC地址,格式为C0-63-94-E1-46-8B:1)函数中会去掉MAC中‘-’并将字母全转为小写;2)用转换得到的dstmac 逐行匹配文件内容,直到完全匹配上;3)开始往下在文件中查找rssi字段,并读出来;4)将rssi字段解析出来,只需字段中的数字部分;5)将rssi按对应关系转换成wifi信号强度值intenstiy。

    原文作者:satellite13
    原文地址: https://blog.csdn.net/u013485792/article/details/52303033
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞