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

📄 controltxt.cpp

📁 此程序为EVC编写的MIS系统
💻 CPP
字号:
// 该类的功能是把.txt文件与数据库文件相互转换。
// 创建时间2004.02.24
// 创建人:苟艳
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "抄表系统.h"
#include "ControlTxt.h"
#include "CreateDB.h"
#include "rapi.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/**********************************************************************/
//抄表记录的各个字段
#define prop_01  L"水表账号"
#define prop_03  L"用户地址"
#define prop_02  L"用户名称"
#define prop_04  L"字轮读数"
#define prop_05  L"抄表人员名称"
#define prop_06  L"抄表日期"
#define prop_07  L"备注"

//////////////////////////////////////////////////////////////////////
extern CCreateDB  database;
//////////////////////////////////////////////////////////////////////

CControlTxt::CControlTxt()
{
	position=0;

}

CControlTxt::~CControlTxt()
{

}

//
TCHAR * CControlTxt::Readtxtfile()   //
{
	char buffer[300];
	//TCHAR data[600];
	//LPWSTR data;
	TCHAR  *data,*point,*head;
	DWORD dwptr;
	HANDLE Txthandle;
	int position=0;
	char c;
	UINT retByt;
	//
	head=data;   //获得数组的头指针
	//
	WIN32_FIND_DATA filedata;
	LPCWSTR lpFileName=L"\\My Documents\\用户档案记录.txt";
	HANDLE filehandle=FindFirstFile(lpFileName,&filedata);
	if(filehandle==INVALID_HANDLE_VALUE)
	{
		::AfxMessageBox(L"该文件夹中无用户档案记录.txt文件!");
		return	NULL;
	}
	else 
	{
		CString testStr;
		int nLen,i=0,length=0;
		Txthandle=CreateFile(lpFileName,GENERIC_WRITE |GENERIC_READ,        //打开.txt文件
		                  0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
		if(Txthandle==INVALID_HANDLE_VALUE)
		{
			::AfxMessageBox(L"用户档案记录打开失败!");
			return NULL;
		}
		dwptr=SetFilePointer(Txthandle,position,NULL,FILE_BEGIN);//文件指针
		CFile fFile((int)Txthandle);
		memset(buffer,0,300);
		retByt=fFile.Read(&c,1);
	    buffer[i++]=c;
		while(retByt>=1)  //文件还没读完
		{
			if(i>300)     //缓存已满
			{
			nLen=MultiByteToWideChar(CP_ACP,0,(char*)buffer,i-1,0,0);//Ansi转换成Unicode的长度
			length+=nLen;
			point=new TCHAR[nLen];
			MultiByteToWideChar(CP_ACP,0,(char*)buffer,i-1,point,nLen);
			wcscpy(data+length-nLen,point);
			memset(buffer,0,300);
			i=0;
			}
			 position+=1;
			dwptr=SetFilePointer(Txthandle,position,NULL,FILE_BEGIN);//文件指针
			retByt=fFile.Read(&c,1);
			buffer[i++]=c;
		}
		if(length==0)
		{
			///Ansi转换成Unicode
			nLen=MultiByteToWideChar(CP_ACP,0,(char*)buffer,i-1,0,0);//Ansi转换成Unicode的长度
			data=new TCHAR[nLen];
			MultiByteToWideChar(CP_ACP,0,(char*)buffer,i-1,data,nLen);
		}
		else
		{	
			nLen=MultiByteToWideChar(CP_ACP,0,(char*)buffer,i-1,0,0);//Ansi转换成Unicode的长度
			length+=nLen;
			point=new TCHAR[nLen];
			MultiByteToWideChar(CP_ACP,0,(char*)buffer,i-1,point,nLen);	
			wcscpy(data+length-nLen,point);
			data=head;
		}
		
		 fFile.Close();
		 CloseHandle(Txthandle);
		 FindClose(filehandle);
	}
	return data;
}
////
/*BOOL CControlTxt::WriteDatabase(TCHAR tmpdata[50],int flag)
{
	CEOID record=-1,oid;
	//CEPROPVAL tempprop;
	CEPROPVAL propval[3];
	HANDLE Writehandle;
	DWORD index;
	CString str;

		if(database.CreateWtableDB (1))    //以水表账号的排序打开抄表记录
				Writehandle=database.writetbhandle ;
		switch(flag)
		{
			case 1: propval[0].propid =MAKELONG(CEVT_LPWSTR,prop_01);  //水表账号
					propval[0].wFlags =0;
					propval[0].val .lpwstr =tmpdata;
					//database.wtableflag =flag;
					//if(database.CreateWtableDB (1))
						//Writehandle=database.writetbhandle ;
					break;
			case 2: 
					propval[1].propid =MAKELONG(CEVT_LPWSTR,prop_02);  //用户名称
					propval[1].wFlags =0;
					propval[1].val .lpwstr =tmpdata;
				///	database.wtableflag =flag;
					//if(database.CreateWtableDB (2))
					//	Writehandle=database.writetbhandle ;
					break;
			case 3:
					propval[2].propid =MAKELONG(CEVT_LPWSTR,prop_03);  //用户地址
					propval[2].wFlags =0;
					propval[2].val .lpwstr =tmpdata;
					//database.wtableflag =flag;
					//if(database.CreateWtableDB (3))
						//Writehandle=database.writetbhandle ;
				//	break;
		}
		//propval[flag-1]=tempprop;
		//oid=CeSeekDatabase(Writehandle,CEDB_SEEK_VALUEFIRSTEQUAL,(DWORD)&tempprop,&index);
	//
	
		//
		if(flag==3)   //向数据库中添加新的记录
		{
			record=CeWriteRecordProps(Writehandle,0,3,propval);
		
			if(record==0)
			{
				int error;
				TCHAR text[64];
				error=GetLastError();
				wsprintf(text,L"Db Write.error=%d(%x)",error,error);
				::AfxMessageBox(text);
				CloseHandle(Writehandle);
				return FALSE;
			}
		}

	//
	CloseHandle(Writehandle);
	return TRUE;
}*/
//
BOOL CControlTxt::Writetxtfile()  //从抄表记录中取出字段数据转换写入文本文件
{
	LPCWSTR filename=L"\\My Documents\\抄表记录.txt";
	CEOID findrecord;
	DWORD index;
	WORD propnumber;
	PCEPROPVAL pRecord=0;
	DWORD cbBuff;
	TCHAR RecordStr[50];
	BOOL result;
	HANDLE Writehandle,txtHandle;
	//
	//把以前存在的抄表记录文本文件清0
	txtHandle=CreateFile(filename,GENERIC_WRITE |GENERIC_READ,        
		                  0,NULL,TRUNCATE_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	CFile fFile((int)txtHandle);
	fFile.Close();
	CloseHandle(txtHandle);
	//


	if(result=database.CreateWtableDB())  ///打开了抄表记录
	{
		Writehandle=database.writetbhandle;

		findrecord=CeSeekDatabase(Writehandle,CEDB_SEEK_BEGINNING,0,&index);
		if(findrecord==0)
		{
			::AfxMessageBox(L"表中无记录!");
			CloseHandle(Writehandle);
			return FALSE;
		}
		findrecord=CeReadRecordProps(Writehandle,CEDB_ALLOWREALLOC,&propnumber,NULL,(LPBYTE*)&pRecord,&cbBuff);
		while(findrecord!=0)
		{
			for(int i=0;i<propnumber;i++)
			{
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_01))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_02))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_03))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_04))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_05))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_06))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				if(pRecord->propid==MAKELONG(CEVT_LPWSTR,prop_07))
				lstrcpy(RecordStr,pRecord->val .lpwstr );
				//
				writeTXT(filename,RecordStr,1);///一个记录的一个属性赋值完了
				//
				pRecord++;
			}
			writeTXT(filename,NULL,2);
			pRecord=0;
			findrecord=CeReadRecordProps(Writehandle,CEDB_ALLOWREALLOC,&propnumber,NULL,(LPBYTE*)&pRecord,&cbBuff);	
		}
	}
	else
		return FALSE;
	CloseHandle(Writehandle);
	return TRUE;
}
//
BOOL CControlTxt::writeTXT(LPCWSTR txtFile,TCHAR data[50],int Flag) //转换文本文件函数
{
	char s;
	unsigned char buffer[125];
	DWORD dwPtr;
	int nLen;
	HANDLE txtHandle;

///////////////////////////////////////////////////////////////////
	txtHandle=CreateFile(txtFile,GENERIC_WRITE |GENERIC_READ,        //创建.txt文件
		                  0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
		if(txtHandle==INVALID_HANDLE_VALUE)
		{
			::AfxMessageBox(L"创建.txt文件失败!");
			return FALSE;
		}
		dwPtr=SetFilePointer(txtHandle,position,NULL,FILE_BEGIN);//文件指针
		CFile fFile((int)txtHandle);
		switch(Flag)
		{
		case 1:
			{
				//从Unicode转换到Ansi
				memset(buffer,0,125);
				nLen=WideCharToMultiByte(CP_ACP,0,data,lstrlen(data),0,0,0,0);//要转化成ANSI的长度
				WideCharToMultiByte(CP_ACP,0,data,lstrlen(data),(char*)buffer,125,0,0);

				for(int i=0;i<strlen((char*)buffer);i++)
				{
					s=buffer[i];
					fFile.Write(&s, 1);
				}
					s=';';
					fFile.Write(&s, 1);
				position+=strlen((char*)buffer)+1;
				break;
			}
		case 2:
			{
				s='/';
				for(int i=0;i<2;i++)
					fFile.Write(&s, 1);
					position+=2;
					break;
			}
	
		}
		fFile.Close();

		CloseHandle(txtHandle);
		return TRUE;
}

⌨️ 快捷键说明

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