?? controltxt.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 + -