一个简单的文件处理--16进制数据统计分析
2011-03-03 23:21
751 查看
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char * str1="offset_lr < offset: ";
const char * str2="offset_lr > offset: ";
#define BUFFERSIZE 128
char StrBuffer[BUFFERSIZE];
#define SIZE 49
int SMALL_COUNT[SIZE];
int LARGE_COUNT[SIZE];
double average=0.0;
double SMALL_SUM=0;
double LARGE_SUM=0;
int totalCount=0;
int strSize=20;
int findAndCount(const char * searchStr,int arr[SIZE] ,bool Large)
{
char * str;
char ch;
int tmp=0;
FILE * f=fopen("test.txt","rb");
if(f==NULL)
{
return (-1);
}
else
{
while(fread(StrBuffer,1,BUFFERSIZE,f)==BUFFERSIZE)
{
str=StrBuffer;
if ((str=strstr(str,searchStr))!=NULL)
{
bool exitFlag=false;
for(int i=2;i<8;i++)
{
ch=*(str+strSize+i);
printf("%c",ch);
if(ch=='/0')
{
exitFlag=true;
break;
}
}
if(exitFlag)
continue;
ch=*(str+strSize+8);
if(ch=='/0')
continue;
printf("%c",ch);
if(ch<='9')
tmp=(ch-'0')*16;
else
tmp=(ch-'A'+10)*16;
ch=*(str+strSize+9);
if(ch=='/0')
continue;
printf("%c",ch);
if(ch<='9')
tmp+=(ch-'0');
else
tmp+=(ch-'A'+10);
if(tmp<SIZE)
{ arr[tmp]++;
printf("/n%d",arr[tmp]);
}
printf("/n");
}
}
for(int i=0;i<SIZE;i++)
{
totalCount+=arr[i];
if(Large)
LARGE_SUM+=(arr[i]*i);
else
SMALL_SUM+=(-arr[i])*i;
printf("%d:%d/n",i,arr[i]);
}
fclose(f);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int i=0;i<SIZE;i++)
{
SMALL_COUNT[i]=0;
LARGE_COUNT[i]=0;
}
printf("%d/n",strSize);
findAndCount(str2,LARGE_COUNT,true);
printf("LARGE_SUM:%lf,SMALL_SUM:%lf/n",LARGE_SUM,SMALL_SUM);
system("pause");
findAndCount(str1,SMALL_COUNT,false);
printf("LARGE_SUM:%lf,SMALL_SUM:%lf/n",LARGE_SUM,SMALL_SUM);
average=(LARGE_SUM+SMALL_SUM)/totalCount;
printf("Total Count:%d/nAverage:%lf/n",totalCount,average);
system("pause");
return 0;
}
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char * str1="offset_lr < offset: ";
const char * str2="offset_lr > offset: ";
#define BUFFERSIZE 128
char StrBuffer[BUFFERSIZE];
#define SIZE 49
int SMALL_COUNT[SIZE];
int LARGE_COUNT[SIZE];
double average=0.0;
double SMALL_SUM=0;
double LARGE_SUM=0;
int totalCount=0;
int strSize=20;
int findAndCount(const char * searchStr,int arr[SIZE] ,bool Large)
{
char * str;
char ch;
int tmp=0;
FILE * f=fopen("test.txt","rb");
if(f==NULL)
{
return (-1);
}
else
{
while(fread(StrBuffer,1,BUFFERSIZE,f)==BUFFERSIZE)
{
str=StrBuffer;
if ((str=strstr(str,searchStr))!=NULL)
{
bool exitFlag=false;
for(int i=2;i<8;i++)
{
ch=*(str+strSize+i);
printf("%c",ch);
if(ch=='/0')
{
exitFlag=true;
break;
}
}
if(exitFlag)
continue;
ch=*(str+strSize+8);
if(ch=='/0')
continue;
printf("%c",ch);
if(ch<='9')
tmp=(ch-'0')*16;
else
tmp=(ch-'A'+10)*16;
ch=*(str+strSize+9);
if(ch=='/0')
continue;
printf("%c",ch);
if(ch<='9')
tmp+=(ch-'0');
else
tmp+=(ch-'A'+10);
if(tmp<SIZE)
{ arr[tmp]++;
printf("/n%d",arr[tmp]);
}
printf("/n");
}
}
for(int i=0;i<SIZE;i++)
{
totalCount+=arr[i];
if(Large)
LARGE_SUM+=(arr[i]*i);
else
SMALL_SUM+=(-arr[i])*i;
printf("%d:%d/n",i,arr[i]);
}
fclose(f);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int i=0;i<SIZE;i++)
{
SMALL_COUNT[i]=0;
LARGE_COUNT[i]=0;
}
printf("%d/n",strSize);
findAndCount(str2,LARGE_COUNT,true);
printf("LARGE_SUM:%lf,SMALL_SUM:%lf/n",LARGE_SUM,SMALL_SUM);
system("pause");
findAndCount(str1,SMALL_COUNT,false);
printf("LARGE_SUM:%lf,SMALL_SUM:%lf/n",LARGE_SUM,SMALL_SUM);
average=(LARGE_SUM+SMALL_SUM)/totalCount;
printf("Total Count:%d/nAverage:%lf/n",totalCount,average);
system("pause");
return 0;
}
相关文章推荐
- 从Apache的日志文件收集和提供统计数据(一个Python插件架构的简单实现)
- 实时计算,流数据处理系统简介与简单分析
- 简单GPS数据处理(gps数据分析)
- 一个简单处理RAR文件的bat
- python数据分析-处理CSV/EXCEL表格文件
- iOS 一个在UDP/TCP通信时,对16进制、10进制、NSData等数据之间的转换发送等处理的工具类库,一行代码将16进制字符串轻松转换成UDP需要的指令数据
- 实时计算,流数据处理系统简介与简单分析
- Mysql源代码分析(7): MYISAM的数据文件处理--转载
- 【ORACLE】一个简单实用的对数据进行行列转换并统计的SQL语句
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- Hive实战:将xml文件处理为txt文件,并用Hive进行微博数据分析
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- ios数据处理 简单文件处理
- PE文件格式分析系列(文章1)----一个PE文件导入表数据的分析(MFC工程调试版)
- 用perl对文件中的字符串做处理的一个简单的示例
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- java对txt文本文件的复制粘贴(对文件内容进行了简单的修改)以行为一个处理单位
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
- 写了一个简单的处理类似xml文件的类,请大家指正【原创】
- 一个简单Class文件的分析