⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getdata_add.cpp

📁 主要是针对一个串口小盒子的分析数据做填补
💻 CPP
字号:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cctype>

using namespace std;

typedef unsigned char uchar;

FILE *fpin;
FILE *fpout;

uchar data[1024];
char word[1024];
char line[1024];


bool isHex(char ch)
{
	return (isdigit(ch) || 'A'<=ch && ch <='F');
}

bool isBlankLine(char *buf)
{
	while(*buf != '\0')
	{
		if(isprint(*(buf++))) return false;
	}
	return true;
}

uchar hexToDec(char hex)
{
	if(isdigit(hex)) return hex-'0';
	else return hex-'A'+10;
	
}

bool isNeedHex(char *buf, int num)
{
	int cnt = 0;
	bool ok = true;
	uchar val=0;
	while(*buf != '\0' && ok && cnt <= num)
	{
		if(isgraph(*buf) && !isspace(*buf))
		{
			if(!isHex(*buf)) return false;
			cnt ++;
			if(cnt%2==1) 
			{
				val = hexToDec(*buf);
			//	printf("val h=%u", val);
				val <<= 4;
			}
			else 
			{
				val += hexToDec(*buf);
				data[cnt/2] = val;
			
			//	printf("val =%02X", data[cnt/2]);
					val = 0;
			}
		}
		++buf;
	}
	
	if(!ok || cnt != num) return ok = false;
	
	memset(line, 0, sizeof(line));
	
	if(fgets(line, 1024, fpin)!=NULL && !isBlankLine(line)) return ok = false;

	return ok;
	
}


int main()
{
start:
	fpin = stdin;
	fpout = stdout;
	
	char fileOut[256];
	char fileIn[256];
	
	puts("file name:");
	scanf("%s", fileIn);
	sprintf(fileOut, "%s.add", fileIn);
	
	if((fpin=fopen(fileIn, "r")) == NULL) 
	{
		fprintf(fpout, "err: can not open: %s to read\n", fileIn);
		return 0;
		//goto end;
	}
	
	if((fpout=fopen(fileOut, "w")) == NULL) 
	{
		fprintf(fpout, "err: can not open %s to write\n", fileOut);
		return 0;
		//goto end;
	}


	char ch;
	
	int num = 0;
	while((ch=fgetc(fpin)) != EOF)
	{
		if(ch == 'U')
		{
			fputc(ch, fpout);
			if((ch=fgetc(fpin))==EOF) goto end;
			if(ch=='n')
			{
				fputc(ch, fpout);
				if((ch=fgetc(fpin))==EOF) goto end;
				if(ch == 'k')
				{
					fputc(ch, fpout);
					int left = 4;
					while(left--)
					{
						if((ch=fgetc(fpin))==EOF) goto end;
						fputc(ch, fpout);
					}
					
					fprintf(fpout, "(%d)", ++num); //
					continue;
					
				}
			}
		
		}
		fputc(ch, fpout);
	}
		
end:

	fclose(fpin);
	fclose(fpout);
	system("pause");
	return 0;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -