C语言对文件操作访问及数据统计分析处理


说明

1. 数据统计分析处理的目的是为了查找文件中相同的数据,并简单的统计分析,主要是熟悉C语言对文件操作访问。
2. 本文中的某些部分是为了演示使用,实际中可去掉。

一、冒泡排序

/* 排序 */
void BubbleSort_U16(u16 *pdata, u32 length)
{
	u32 i, j;
	u16 temp;

	for (i = 0; i < (length - 1); i++)
	{
		for (j = (i + 1); j < length; j++)
		{
			if (*(pdata + j) < *(pdata + i))
			{
				temp = *(pdata + i);
				*(pdata + i) = *(pdata + j);
				*(pdata + j) = temp;
			}
		}
	}
}

二、求平均值

/* 求平均值 */
u16 Average_u16(u16 *pdata, u16 length, u16 ignore)
{
	u32 nX;
	u32 temp = 0;

	for (nX = ignore; nX < (length - ignore); nX++)
	{
		temp += pdata[nX];
	}
	temp = (temp * 10) / (length - (ignore << 1));
	temp = (temp + 5) / 10;

	return (u16)temp;
}

三、集中度分析

/* 数据集中度分析 */
static u16 usConcentrate_Analyse_SingleData(u16 *pDat, u16 len, u16 startIndex, u16 gapVal, u16 actCount)
{
	u16 i = 0;
	u16 index = 0;
	u16 resDat = 0;
	u16 outNum = 0;
	u8  num = 1;
	u8  valid = 0;


	if (len < actCount)
	{
		return 0;
	}

	outNum = actCount;
	index = startIndex;
	for (i = startIndex; i < (startIndex + len - 1); i++)
	{
		if ((pDat[i + 1] - pDat[index]) <= gapVal)
		{
			num++;
			valid = 1;
		}
		else
		{
			valid = 0;
		}


		if ((!valid) || (i == (startIndex + len - 2)))
		{
			if ((num >= actCount) && (num >= outNum))
			{
				outNum = num;
				resDat = Average_u16(&pDat[index], num, 0);
			}

			index = i + 1;
			num = 1;
		}
	}

	return resDat;
}

四、数据统计分析处理

#define READ_FILE_DIR 								"HandlerData/7#近距离拟合白靶-60-260-120V-2023-01-31.log"	/* 读源文件 */
#define WRITE_FILE_DIR 								"7#近距离拟合白靶-60-260-120V-2023-01-31.txt"			/* 写目标文件 */
#define RFILE_HEAD									"] OD:%d"	/* 匹配头部 */

#define MEMORY_WRITE_PW_STA_FILE_DIR				"mem_pw_sta.txt"	/* 脉宽统计缓存文件 */
#define MEMORY_WRITE_RES_STA_FILE_DIR				"mem_res_sta.txt"	/* 结果统计缓存文件 */

#define HANDLER_DAT_DIST_MIN						(550)	/* 距离最小值 */
#define HANDLER_DAT_DIST_MAX						(750)	/* 距离最大值 */

#define HANDLER_DAT_PW_MIN							(500)	/* 最小脉宽 */
#define HANDLER_DAT_STA_COUNT_MIN					(10)	/* 最小数量 */
void vData_Analyse_Statistics_Handler(void)
{
	char headDat[100] = { 0 };
	char rMemDat[512] = { 0 };
	u16  memPW[80000] = { 0 };
	char * pChar = NULL;
	char * pHead = NULL;
	u16 actMemCount = 0;
	u16 i = 0, j = 0;
	u64 sumPW = 0;

	FILE * pSourceRead = NULL;					/* 源文件       */
	FILE * pTargetWrite = NULL; 				/* 目标文件     */
	FILE * pMemoryWrite_PulseWidthSta = NULL;	/* 脉宽统计缓存 */
	FILE * pMemoryWrite_ResultSta = NULL;		/* 结果统计缓存 */


	/* 打开文件(源文件) + 读文件 */
	pSourceRead = fopen((const char *)READ_FILE_DIR, "r");
	if (pSourceRead == NULL)
	{
		printf("Open Source File Fail...\r\n");
		return;
	}

	/* 目标文件 */
	pTargetWrite = fopen((const char *)WRITE_FILE_DIR, "w+");
	if (pTargetWrite == NULL)
	{
		printf("Open Targe File Fail...\r\n");
		return;
	}


	/* 脉宽统计缓存文件 */
	pMemoryWrite_PulseWidthSta = fopen((const char *)MEMORY_WRITE_PW_STA_FILE_DIR, "w+");
	if (pMemoryWrite_PulseWidthSta == NULL)
	{
		printf("Open Pulse Width Statistics File Fail...\r\n");
		return;
	}

	/* 结果统计缓存文件 */
	pMemoryWrite_ResultSta = fopen((const char *)MEMORY_WRITE_RES_STA_FILE_DIR, "w+");
	if (pMemoryWrite_ResultSta == NULL)
	{
		printf("Open Result Statistics File Fail...\r\n");
		return;
	}


	printf("Start Handler Data...\r\n");
	fprintf(pTargetWrite, "%s Statistics Result:\r\r\r", WRITE_FILE_DIR);
	for (i = HANDLER_DAT_DIST_MIN; i <= HANDLER_DAT_DIST_MAX; ++i)	/* 遍历距离 */
	{
		/* 文件流指向头部 */
		int index = fseek(pSourceRead, 0, SEEK_SET);
		if (!index)
		{
			/* 格式化比较头部数据 */
			sprintf(headDat, RFILE_HEAD, i);
			while (1)
			{
				/* 获取一行数据 */
				pChar = fgets(rMemDat, sizeof(rMemDat), pSourceRead);

				/* 数据有效 */
				if (pChar != NULL)
				{
					/* 匹配头部数据 */
					pHead = strstr(rMemDat, headDat);

					/* 匹配头部成功 */
					if (pHead != NULL)
					{
						/* 匹配距离和脉宽位置 */
						char * pDist = strstr(rMemDat, "OD:");
						char * pPw   = strstr(rMemDat, "OPW:");

						/* 匹配成功 */
						if ((pDist != NULL) && (pPw != NULL))
						{
							char byteDist[6] = { 0 };
							char bytePW[6]   = { 0 };

							/* 计算数据长度 */
							u8 lenDist = (pPw - pDist) - 3;
							u8 lenPW   = strlen(pPw) - 4;

							/* 获取距离和脉宽数据 */
							memcpy(byteDist, pDist + 3, lenDist);
							memcpy(bytePW,   pPw + 4,   lenPW);

							/* 数据转化 */
							int dist = atoi(byteDist);
							if (dist == i)
							{
								/* 数据转化 */
								int pw = atoi(bytePW);
								if (pw >= HANDLER_DAT_PW_MIN)
								{
									/* 保持数据 */
									memPW[actMemCount++] = pw;
								}
							}
						}
					}
				}
				else
				{
					break;
				}
			}


			/* 数量有效 */
			if (actMemCount >= HANDLER_DAT_STA_COUNT_MIN)
			{
				BubbleSort_U16(memPW, actMemCount);
				for (j = 0; j < actMemCount; ++j)
				{
					sumPW += memPW[j];
				}

				/* 三种平均值 */
				u16 avg1 = (sumPW / actMemCount);
				u16 avg2 = usConcentrate_Analyse_SingleData(memPW, actMemCount, 0, 500, 5);
				u16 avg3 = Average_u16(memPW, actMemCount, (actMemCount >> 3));

				/* 打印输出 */
				printf("[%04d] Max:%-6d  Min:%-6d  Avg1:%-5d  Avg2:%-5d  Avg3:%-5d  ABS:%-5u  Total:%-5d\r\n",
					i, memPW[actMemCount - 1], memPW[0], avg1, avg2, avg3, \
					(memPW[actMemCount - 1] - memPW[0]), actMemCount);

				/* 写入到文件中 */
				fprintf(pTargetWrite, "[%04d] Max:%-6d  Min:%-6d  Avg1:%-5d  Avg2:%-5d  Avg3:%-5d  ABS:%-5u  Total:%-5d\r",
					i, memPW[actMemCount - 1], memPW[0], avg1, avg2, avg3, \
					(memPW[actMemCount - 1] - memPW[0]), actMemCount);
			}

			actMemCount = 0;
			sumPW       = 0;
		}
	}


	/* 在末尾写入时间日期 */
	fprintf(pTargetWrite, "\r\r\rCompiler Date: %s  %s\r", __DATE__, __TIME__);


	/**************************************** 关闭文件 ****************************************/
	int closeRes = 0;
	if (pSourceRead != NULL)
	{
		closeRes = fclose(pSourceRead);
		pSourceRead = NULL;
		if (closeRes < 0)
		{
			printf("Close Source File Fail...\r\n");
		}
	}

	if (pTargetWrite != NULL)
	{
		fclose(pTargetWrite);
		pTargetWrite = NULL;
		if (closeRes < 0)
		{
			printf("Close Target File Fail...\r\n");
		}
	}

	if (pMemoryWrite_PulseWidthSta != NULL)
	{
		closeRes = fclose(pMemoryWrite_PulseWidthSta);
		pMemoryWrite_PulseWidthSta = NULL;
		if (closeRes < 0)
		{
			printf("Close Pulse Width Statistics File Fail...\r\n");
		}
	}

	if (pMemoryWrite_ResultSta != NULL)
	{
		closeRes = fclose(pMemoryWrite_ResultSta);
		pMemoryWrite_ResultSta = NULL;
		if (closeRes < 0)
		{
			printf("Close Result Statistics File Fail...\r\n");
		}
	}



	/**************************************** 删除文件 ****************************************/
	int rmRes = 0;
	rmRes = remove(MEMORY_WRITE_PW_STA_FILE_DIR);
	if (rmRes < 0)
	{
		printf("Delete Pulse Width Statistics File Fail...\r\n");
	}

	rmRes = remove(MEMORY_WRITE_RES_STA_FILE_DIR);
	if (rmRes < 0)
	{
		printf("Delete Result Statistics File Fail...\r\n");
	}
}

五、演示例程

int main(int argc, char * argv[])
{
	vData_Analyse_Statistics_Handler();
	
	printf("\r\n\r\n Compiler Date: %s  %s\r\n", __DATE__, __TIME__);
	while (1);
	return 0;
}

将要读取的源文件内容
在这里插入图片描述

六、处理后结果

7#近距离拟合白靶-60-260-120V-2023-01-31.txt Statistics Result:


[0552] Max:9698    Min:9484    Avg1:9591   Avg2:9591   Avg3:9592   ABS:214    Total:12   
[0553] Max:9699    Min:9451    Avg1:9573   Avg2:9573   Avg3:9574   ABS:248    Total:50   
[0554] Max:9734    Min:9468    Avg1:9621   Avg2:9622   Avg3:9626   ABS:266    Total:40   
[0555] Max:10676   Min:9463    Avg1:9658   Avg2:9635   Avg3:9641   ABS:1213   Total:44   
[0556] Max:9763    Min:9504    Avg1:9662   Avg2:9663   Avg3:9665   ABS:259    Total:56   
[0557] Max:9772    Min:9552    Avg1:9696   Avg2:9697   Avg3:9703   ABS:220    Total:40   
[0561] Max:9794    Min:9644    Avg1:9737   Avg2:9738   Avg3:9739   ABS:150    Total:28   
[0562] Max:9839    Min:9570    Avg1:9726   Avg2:9727   Avg3:9729   ABS:269    Total:37   
[0563] Max:9806    Min:9615    Avg1:9735   Avg2:9735   Avg3:9738   ABS:191    Total:53   
[0564] Max:9799    Min:9635    Avg1:9751   Avg2:9751   Avg3:9755   ABS:164    Total:55   
[0565] Max:9806    Min:9694    Avg1:9760   Avg2:9761   Avg3:9763   ABS:112    Total:51   
[0566] Max:9835    Min:9670    Avg1:9747   Avg2:9748   Avg3:9747   ABS:165    Total:18   
[0570] Max:9847    Min:9721    Avg1:9801   Avg2:9802   Avg3:9805   ABS:126    Total:41   
[0571] Max:9858    Min:9750    Avg1:9812   Avg2:9812   Avg3:9813   ABS:108    Total:67   
[0572] Max:9846    Min:9728    Avg1:9806   Avg2:9807   Avg3:9808   ABS:118    Total:36   
[0573] Max:9862    Min:9750    Avg1:9815   Avg2:9815   Avg3:9817   ABS:112    Total:47   
[0574] Max:9854    Min:9759    Avg1:9817   Avg2:9817   Avg3:9818   ABS:95     Total:47   
[0579] Max:9876    Min:9798    Avg1:9832   Avg2:9833   Avg3:9832   ABS:78     Total:56   
[0580] Max:9862    Min:9816    Avg1:9840   Avg2:9841   Avg3:9841   ABS:46     Total:41   
[0581] Max:9888    Min:9812    Avg1:9844   Avg2:9844   Avg3:9844   ABS:76     Total:51   
[0582] Max:9866    Min:9821    Avg1:9844   Avg2:9845   Avg3:9845   ABS:45     Total:39   
[0583] Max:9871    Min:9816    Avg1:9843   Avg2:9843   Avg3:9843   ABS:55     Total:50   
[0588] Max:9866    Min:9820    Avg1:9845   Avg2:9846   Avg3:9845   ABS:46     Total:44   
[0589] Max:9866    Min:9818    Avg1:9844   Avg2:9845   Avg3:9845   ABS:48     Total:41   
[0590] Max:9869    Min:9809    Avg1:9843   Avg2:9843   Avg3:9843   ABS:60     Total:44   
[0591] Max:9862    Min:9819    Avg1:9842   Avg2:9842   Avg3:9842   ABS:43     Total:49   
[0592] Max:9869    Min:9811    Avg1:9839   Avg2:9840   Avg3:9839   ABS:58     Total:48   

.......
.......
.......

[0734] Max:9045    Min:8958    Avg1:9003   Avg2:9004   Avg3:9004   ABS:87     Total:45   
[0735] Max:9046    Min:8935    Avg1:8988   Avg2:8988   Avg3:8987   ABS:111    Total:48   
[0736] Max:9036    Min:8909    Avg1:8985   Avg2:8985   Avg3:8984   ABS:127    Total:48   
[0737] Max:9024    Min:8920    Avg1:8978   Avg2:8978   Avg3:8979   ABS:104    Total:50   
[0738] Max:9016    Min:8930    Avg1:8978   Avg2:8978   Avg3:8979   ABS:86     Total:24   
[0742] Max:8995    Min:8899    Avg1:8936   Avg2:8936   Avg3:8935   ABS:96     Total:14   
[0743] Max:8974    Min:8889    Avg1:8936   Avg2:8936   Avg3:8936   ABS:85     Total:49   
[0744] Max:8975    Min:8839    Avg1:8923   Avg2:8923   Avg3:8923   ABS:136    Total:55   
[0745] Max:8976    Min:8863    Avg1:8913   Avg2:8914   Avg3:8914   ABS:113    Total:53   
[0746] Max:8968    Min:8846    Avg1:8905   Avg2:8906   Avg3:8906   ABS:122    Total:47   
[0747] Max:8960    Min:8851    Avg1:8908   Avg2:8909   Avg3:8909   ABS:109    Total:25   



Compiler Date: Mar 14 2023  14:44:39
   

在这里插入图片描述

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值