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

代码实现如下:

/*****************************************************************************
 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

 

点赞