逐行读取文本文件中数据,并保存在数组中;

代码实现如下:

/*****************************************************************************
 Prototype    : ReadParaCfgFile
 Description  : 读取使能配置文件
 Input        : None
 Output       : None
 Return Value : None
 Calls        :
 Called By    :

  History        :
  1.Date         : 
    Author       : 
    Modification : Created function

*****************************************************************************/
int ReadParaCfgFile()
{
    char   aucCfgParaName[FILE_PATH_LEN] = {0};
    char   ascLineStr[MAX_CHAR_NUM_PER_LINE] = {0}; /* 临时保存每行读取出来的字符*/
    FILE*  fpHandle = NULL;
    char*  pKeyCharPos = NULL;
    int  swLen;
    int swValue;
    unsigned int uwValue;
    unsigned int uwLineNum = 0;

	char aucFileInPath[FILE_PATH_LEN] = {0};
	
	unsigned int *uwAddress = (unsigned int *)&CcaCellFlag; //读取数据的存放地址
	
	strcpy(aucFileInPath, gacFilePath);

    //读入使能配置文件
    swLen = sprintf(aucCfgParaName, "%s/%s", aucFileInPath, "xxx.txt");
    if (0 >= swLen)
    {      
        return FAILURE;
    }

    fpHandle = fopen(aucCfgParaName, "r");
    if (NULL == fpHandle)
    {  
        return FAILURE;
    }

    while(!feof(fpHandle))
    {
        memset((void *)ascLineStr, 0, sizeof(ascLineStr));

        fgets(ascLineStr, (sizeof(ascLineStr)-1), fpHandle);

        /* 跳过以//开头的行注释 */
        pKeyCharPos = strstr(ascLineStr, "//");
        if ((NULL != pKeyCharPos) && ('/' == ascLineStr[0]))
        {
            continue;
        }

        /* 跳过空行 */
        if (('\n' == ascLineStr[0]) || ('\r' == ascLineStr[0]))
        {
            continue;
        }                       

        if (ascLineStr[0] == '-')
        {
            swValue= strtol(ascLineStr, NULL, 10);           
			memcpy(uwAddress, &swValue, sizeof(INT32));
        }
        else if (ascLineStr[0] == '0' && ((ascLineStr[1] == 'x') || (ascLineStr[1] == 'X')))
         {
            uwValue= strtoul(ascLineStr, NULL, 16);       
			memcpy(uwAddress, &uwValue, sizeof(UINT32));
        }
        else
        {
            uwValue= strtoul(ascLineStr, NULL, 10);        
			memcpy(uwAddress, &uwValue, sizeof(UINT32));
        }
        
		uwAddress++;
        uwLineNum++;
        /* 检查文件行数是否超过约束 */
        if(uwLineNum  > MAX_HIGH_LEVEL_PARA_NUM)
        {
            message(EN_INFO, "ERROR: read file data to buffer full!!");
            fclose(fpHandle);
            return ERR_BUFF_FULL;
        }
    }
    fclose(fpHandle);

    return SUCCESS;

}

xxx.txt文件格式如下:

// 注释说明

数值

注://为注释,下一行为数值
//uwRxCellNum
0
//uwCellAntEnIdx
2
//uwRxRxEnd
0	 
//uwRxTxEnd
0	 
//uwRxRxEn
1	 
//uwRxTxEn
1	 
//uwRxPpduNum;     
1
//uwRxRfNum;       
0
//uwRxLockNum;     
60

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值