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

📄 txtlib.cpp

📁 在WINDOWS把文本文件转化成二进制文件
💻 CPP
字号:
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
double  contwrite( char *FileName1,char *FileName2);
char *searchid(char *id,char *fp);
int  Count_Lines( char *FileName)
{
    int  count=0;
	FILE *FP,*fp3;
	int len=0,i=0;
	char contxt[1024];
	char *fname,define[64],defid[32];
	fname=FileName;
	if((FP=fopen(fname,"rb"))==NULL)   
	{     
       printf("\n失败!不能打开文件!按任意键退出……");   
       getch();   
       exit(1);   
	}   
	if((fp3=fopen("define.h","wb"))==NULL)   
	{     
       printf("\n失败!不能打开源文件!按任意键退出……");   
       getch();   
       exit(1);   
	}   
	while(!feof(FP))
	{
		fgets(contxt,1024,FP);
		if(contxt[0]==';')continue;
		if(contxt==NULL)break;
		count++;
		sscanf(contxt,"%[^::]",defid);//取的所读行的ID,0x60,0x60,0x40,0x01,读到空格就停止
		sprintf(define,"%s  %s  0x%x\n","#define",defid,count);
		fputs(define,fp3);
		memset(define,0,sizeof(define));
	}
  return count;	
}

int main(int argc, char **argv)
{
	FILE   *fp1,*fp2;
	int  count_lines=0,count=0;
	unsigned int beginPos=0,currentpos=0;
    char txt[4]="";  
    unsigned int strLen;
	char strBuffer[1024]="";
	int len=0,i=0,j=0;
    if((fp1=fopen(argv[1],"rb"))==NULL)   
	{     
       printf("\n失败!不能打开源文件(1)!按任意键退出……");   
       getch();   
       exit(1);   
	}   
    if((fp2=fopen(argv[2],"wb"))==NULL)   
	{   
	  printf("\n失败!不能打开目标文件(2)!按任意键退出……");   
	  fclose(fp1);   
	  getch();   
	  exit(1);   
	}  
	count_lines=Count_Lines(argv[1]);
	beginPos = (unsigned int)(count_lines)*4;
	currentpos=	beginPos;
	while(!feof(fp1))
	{
			memset(strBuffer,0,sizeof(strBuffer));
			fgets(strBuffer,1024,fp1);
			count++;
			if(strBuffer[0]==';')continue;
			if(count>count_lines)break;
			strLen = strlen(strBuffer)+1;		
			for(j=0;j<4;j++)
			{   
				txt[j]=(currentpos & (0xFF<<8*(3-j)))/(1 << 8 * (3 - j));//有问题,如果刚好碰到OA表示换行的字符的时候系统回自动加成OD 
			}
			fwrite(txt, 1, 4, fp2);//write sddres
			currentpos += strLen;
	}
	contwrite(argv[2],argv[1]);//write contend
	fclose(fp1); 
	fclose(fp2);   
	printf("\n转换完毕,按任意键退出……");  
	getch(); 
    exit(1); 
	//*searchid("0x60604004",argv[2]);//searchid 
	return 0;   
}

double  contwrite( char *FileName1,char *FileName2)
{
    int  count=0,i=0,num=0;
	FILE *FP,*fp2;
	unsigned int strLen;
	char strBuffer[1024];
    int m=0,n=0,j=0;
	char *fname1,*fname2;
	unsigned int beginPos=0,currentpos=0;
	fname1=FileName1;
	fname2=FileName2;
	count = Count_Lines(fname2);
	if((FP=fopen(fname1,"wb"))==NULL)   
	{     
       printf("\n失败!不能打开源文件!按任意键退出……");   
       getch();   
       exit(1);   
	} 
	if((fp2=fopen(fname2,"rb"))==NULL)   
	{     
       printf("\n失败!不能打开源文件!按任意键退出……");   
       getch();   
       exit(1);   
	} 
	beginPos = (unsigned int)(count)*4;
	currentpos=	beginPos;
	while(!feof(fp2))
		{		
				memset(strBuffer,0,sizeof(strBuffer));
				fgets(strBuffer,1024,fp2);
				if(strBuffer[0]==';')continue;
				num++;
				if(num>count)break;
				strLen = strlen(strBuffer);
				fseek(FP,currentpos,SEEK_SET);
				fwrite(strBuffer,1,strLen,FP);
				currentpos += strLen;
		}
  return  num;	
}

char *searchid(char *id,char * fp)
{
	FILE *fp1;
	char data[4]={0};
	unsigned int beginposbin=0,currentposbin=0;
	//char a1[10]; 
//	char a2[10];
//	char *strid;
	 char addr[4];
	unsigned int value=0,value1=0;
	unsigned int count=0;
	//int j=0;
	if((fp1=fopen(fp,"rb"))==NULL)   
	{     
       printf("\n失败!不能打开源文件(1)!按任意键退出……");   
       getch();   
       exit(1);   
	} 
//	fread(data,4,1,fp1);
	//value = data[0]<<24+data[1]<<16+data[2]<<8+(data[3]);
	//value = (unsigned int)(data[0] << 24) + (unsigned int)(data[1] << 16) + (unsigned int)(data[2] << 8) + (unsigned int)data[3];
	/*for (int i = 0; i < 4; i++)
	{
		value += (unsigned int)(data[i] << (24 - 8*i));
	}
	count = (value)/4;*/
	//fclose(fp1);
	//for(unsigned int m=0;m<count;m++)
	fread(addr,4,1,fp1);//
	for (int j = 0; j < 4; j++)
	{
		value1 += (unsigned int)(addr[j] << (24 - 8*j));
	}
	beginposbin = value1+1;///why need +1?
	while(!feof(fp1))
	{   int p=0,q=0,d=0,count = 0,k=0;
		char readbuffer[1024];
		char destbuffer[1024];
		char resousbuffer[1024],idbuffer[8];
		unsigned int result=0,resultpos=0;
		unsigned int strlenbin=0,comparevalue=0;
	    char compareid[11];
		fseek(fp1,beginposbin,SEEK_SET);
		fgets(readbuffer,1024,fp1);
		strlenbin=strlen(readbuffer);
		sscanf(readbuffer,"%[^ ]",resousbuffer);//取的所读行的ID,0x60,0x60,0x40,0x01,读到空格就停止
		for(p=0;p<20;p++)
		{	
			if(resousbuffer[p]==',')
			{
				count++;
				idbuffer[(p-2)-(2+3*(count-1))]=resousbuffer[p-2];//抽取ID的数字,60604001
				idbuffer[(p-1)-(2+3*(count-1))]=resousbuffer[p-1];
			}
		}
		for(q=2;q<10;q++)//变成标准ID,0x60604001
		{
			compareid[0]='0';
			compareid[1]='x';
			compareid[q] = idbuffer[q-2];
			compareid[10]=0;
		}
		if(!strcmp(compareid,id))//取ID对应的数据内容
		{
			resultpos = strlen(resousbuffer);
			fseek(fp1,value1+resultpos+1,SEEK_SET);
			fgets(destbuffer,1024,fp1);
			return destbuffer;//返回值用的时候要处理,局部变量是不能返回的。
		}
		beginposbin+=strlenbin;
	}
/*	strid = id;
	strncpy(a1,strid+strlen(strid)-3,3); 
	*(a1+3)='\0'; */
	return 0;
}

⌨️ 快捷键说明

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