|
firstsail
高级用户
积分 668
发帖 295
注册 2005-7-26 来自 广东深圳
状态 离线
|
『楼 主』:
无须版权的数据库,Office Excel能识别打开
如何定制可移值的Office Excel能识别的数据库呢?
在单片机系统,DOS系统或Linux系统中,经常要用到自定义的数据库,该数据库文件希望能被Windows下的Office Excel识别,怎么办呢?现在有一个方法,可以使用Foxpro2.6版本的数据库,文件结构是公开的,且可被Office Excel识别。
在WinSail中,由Symbol2.h头文件,Foxpro.h头文件和Foxpro.Cpp源文件组成,稍后会公开所有源代码上来,如果有需要的人士可以移值!
[ Last edited by firstsail on 2018-7-14 at 19:32 ]
|
|
2018-7-14 12:40 |
|
|
firstsail
高级用户
积分 668
发帖 295
注册 2005-7-26 来自 广东深圳
状态 离线
|
『第
2 楼』:
//下面是Symbol2.h文件
#ifndef _SYMBOL2_H_
#define _SYMBOL2_H_
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif
#define _fmemcpy memcpy
#define _fstrcpy strcpy
#define _fstricmp stricmp
#define _fstrcmp strcmp
#define _fstrlen strlen
#define _fmemset memset
#define farmalloc malloc
#define farfree free
#define pi 3.1415926
#define CONST_SINGLE_T 3.14159f
#define CONST_DOUBLE_T 3.14159265359
typedef char* LPCTSTR;
typedef void* LPVOID;
typedef int COLOR;
typedef int HANDLE;
typedef unsigned int WORD;
typedef unsigned char BYTE;
typedef int BOOL;
typedef unsigned long DWORD;
typedef DWORD SORT;
typedef int BOOL;
typedef unsigned int BOOLEN;
typedef int HWND;
typedef DWORD WPARAM;
typedef DWORD LPARAM;
typedef long LONG;
typedef long* LPLONG;
typedef void VOID;
typedef DWORD ULONG;
typedef float FLOAT;
typedef int INT;
typedef signed short SHORT;
typedef unsigned short USHORT;
typedef unsigned short WCHAR;
#define DELETE(a) {if ((a) != NULL) delete (a); (a) = NULL;}
#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)
#define SHIFT8(a) ((a)<<8)
#define SHIFT16(a) ((a)<<16)
#define LOBYTE(a) ((BYTE)((a)&0xFF))
#define HIBYTE(a) ((((DWORD)(a))>>8)&0xFF)
#define LOWORD(a) ((WORD)((WORD)(((DWORD)(a))&0xFFFFL)))
#define HIWORD(a) ((WORD)((((DWORD)(a))>>16)&0xFFFFL))
#endif
|
|
2018-7-14 12:42 |
|
|
firstsail
高级用户
积分 668
发帖 295
注册 2005-7-26 来自 广东深圳
状态 离线
|
『第
3 楼』:
//下面文件是Foxpro.h文件
#ifndef _FOXPRO_20110811_H_
#define _FOXPRO_20110811_H_
//数据库头
struct FOXPRO_HEAD
{
BYTE byFlags;
BYTE byYear;
BYTE byMonth;
BYTE byDay;
DWORD dwRecordCount;
WORD wRecordAddress;
WORD wRecordLength;
DWORD dwReserve1;
DWORD dwReserve2;
DWORD dwReserve3;
DWORD dwReserve4;
DWORD dwReserve5;
};
//数据库“域”属性
struct FOXPRO_FIELDS
{
BYTE strName[10];
BYTE byFill;
BYTE byStyle;
DWORD dwDataAddress;
BYTE byLength;
BYTE byDotCount;
WORD wReserve1;
BYTE byFlags;
BYTE wsReserve[11];
};
//数据库“类”定义
class CFoxpro
{
private:
BOOL m_bNew;
long m_lRecordPoint;
char m_strFile[256];
FOXPRO_HEAD m_FoxproHead;
FOXPRO_FIELDS m_FoxproFields;
FILE* m_pFile;
BYTE* m_pRecordBuf;
FOXPRO_FIELDS* m_pFoxproFields;
public:
//建立新的数据库
BOOL CreateFoxpro(const char*pStr);
//打开已存在的数据库
BOOL OpenFoxpro(const char*pStr);
//关闭数据库
BOOL CloseFoxpro();
//增加一域
BOOL AddFields(const char* strName,BYTE byStyle,int nLength,int nDot);
//获得指定列的“域”属性
FOXPRO_FIELDS* GetFoxproFields(int nIndex);
public:
//获得列的数量
int GetFieldsCount() {return(m_FoxproHead.wRecordAddress-sizeof(FOXPRO_HEAD))/sizeof(FOXPRO_FIELDS);}
//获得纪录总数
long GetRecordCount() {return(m_FoxproHead.dwRecordCount);}
//获得纪录地址
long GetRecordAddress() {return((DWORD)m_FoxproHead.wRecordAddress);}
//获得数据库的最后修改日期-“有”
int GetMonth() {return((int)m_FoxproHead.byMonth);}
//获得数据库的最后修改日期-“日”
int GetDay() {return((int)m_FoxproHead.byDay);}
//获得数据库的最后修改日期-“年”
int GetYear() {return((int)1900+(DWORD)m_FoxproHead.byYear);}
//获得纪录的长度
int GetRecordLength(){return((int)(DWORD)m_FoxproHead.wRecordLength);}
//显示数据库调试信息
void Debug();
public:
//修剪指定字符串的前导空格
static char* TrimLeft(char *pStr);
//修剪指定字符串的后导空格
static char* TrimRight(char *pStr);
public:
//设置串类型“域”的字符串
BOOL SetFieldsString(int nIndex, const char* pStr);
//设置数字类型“域”的数字值
BOOL SetFieldsNumber(int nIndex,float fValue);
//设置日期类型“域”的日期值
BOOL SetFieldsDate(int nIndex,int nYear,int nMonth,int nDay);
//设置逻辑类型“域”的逻辑值
BOOL SetFieldsLogic(int nIndex,BOOL bLogic);
//设置备注类型“域”的备注值
BOOL SetFieldsMemo(int nIndex, const char* pStr);
//获得串类型“域”的字符串
BOOL GetFieldsString(int nIndex,char* pStr);
//获得数字类型“域”的数字值
float GetFieldsNumber(int nIndex);
//获得日期类型“域”的日期值
BOOL GetFieldsDate(int nIndex,int* pnYear,int* pnMonth,int* pnDay);
//获得逻辑类型“域”的逻辑值
BOOL GetFieldsLogic(int nIndex);
//获得备注类型“域”的备注值
BOOL GetFieldsMemo(int nIndex,char* pStr);
//定位纪录
BOOL Seek(long nIndex,int nMode);
//判断数据库是否已经打开
BOOL IsOpen();
//刷新数据库到磁盘
BOOL Flush();
//增加一新纪录
BOOL AddNew();
//准备修改当前纪录
BOOL Edit();
//使当前的更改有效
BOOL Update();
//在当前纪录中插入一条纪录
BOOL Insert();
//删除当前纪录(带上删除标记)
BOOL Delete();
//删除当前纪录
BOOL DeleteEx();
//将带有删除标准的纪录删除
BOOL Zap();
public:
void CoFoxpro();//构造函数调用
//构造函数
CFoxpro();
//构造函数(打开指定的数据库)
CFoxpro(char* strFile);
//析构函数
~CFoxpro();
};
#endif
|
|
2018-7-14 12:44 |
|
|
firstsail
高级用户
积分 668
发帖 295
注册 2005-7-26 来自 广东深圳
状态 离线
|
『第
4 楼』:
//下面文件是Foxpro.Cpp文件
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include <dir.h>
#include <conio.h>
#include <string.h>
#include <sys\\stat.h>
#include <io.h>
#include "symbol2.h"
#include "Foxpro.h"
//#include <Myframe.h>
void CFoxpro::CoFoxpro()
{
m_bNew = FALSE;
m_lRecordPoint = 0;
m_pRecordBuf = NULL;
m_pFile = NULL;
m_pFoxproFields = NULL;
_fmemset(&m_FoxproHead ,0,sizeof(FOXPRO_HEAD));
_fmemset(&m_FoxproFields,0,sizeof(FOXPRO_FIELDS));
_fmemset(&m_strFile,0,256);
}
CFoxpro::CFoxpro()
{
this->CoFoxpro();
}
CFoxpro::CFoxpro(char* strFile)
{
this->CoFoxpro();
this->OpenFoxpro(strFile);
}
CFoxpro::~CFoxpro()
{
if (m_pFile != NULL)
{
fclose(m_pFile);
}
m_pFile = NULL;
DELETE(m_pRecordBuf);
DELETE(m_pFoxproFields);
}
void CFoxpro::Debug()
{
char buf[512];
if(!m_pRecordBuf) return;
int nFields = this->GetFieldsCount();
printf("\n-------------------------------------------------");
for(int i=0;i<nFields;i++)
{
m_FoxproFields = *(this->GetFoxproFields(i));
printf("\n%10s--",m_FoxproFields.strName);
if(m_FoxproFields.byStyle=='C')
{
this->GetFieldsString(i,buf);
this->TrimLeft(buf);
this->TrimRight(buf);
printf("%s",buf);
}
else if(m_FoxproFields.byStyle=='L')
{
BOOL bLogic = this->GetFieldsLogic(i);
printf("%d",bLogic);
}
else if(m_FoxproFields.byStyle=='D')
{
int nYear,nMonth,nDay;
this->GetFieldsDate(i,&nYear,&nMonth,&nDay);
printf("%04d-%02d-%02d",nYear,nMonth,nDay);
}
else if(m_FoxproFields.byStyle=='N')
{
float fValue = this->GetFieldsNumber(i);
if(m_FoxproFields.byDotCount==0) printf("%.0f",fValue);
else if(m_FoxproFields.byDotCount==1) printf("%.1f",fValue);
else if(m_FoxproFields.byDotCount==2) printf("%.2f",fValue);
else if(m_FoxproFields.byDotCount==3) printf("%.3f",fValue);
else if(m_FoxproFields.byDotCount==4) printf("%.4f",fValue);
else if(m_FoxproFields.byDotCount==5) printf("%.5f",fValue);
else printf("%.6f",fValue);
}
else if(m_FoxproFields.byStyle=='M')
{
}
if(i&& !(i%24))
{
getch();
}
}
}
BOOL CFoxpro::CreateFoxpro(const char *pStr)
{
if(m_pFile) return(FALSE);
m_pFile = NULL;
CoFoxpro();
int nFields = 0;
date mDate;
::getdate(&mDate);
m_FoxproHead.byFlags = 0x03;
m_FoxproHead.byDay = (BYTE)mDate.da_day;
m_FoxproHead.byMonth = (BYTE)mDate.da_mon;
m_FoxproHead.byYear = (BYTE)(WORD)(mDate.da_year-1900);
m_FoxproHead.dwRecordCount = 0x00;
m_FoxproHead.wRecordAddress = sizeof(FOXPRO_HEAD)+1;
m_FoxproHead.wRecordLength = 0x01;
m_FoxproHead.dwReserve1 = 0x00;
m_FoxproHead.dwReserve2 = 0x00;
m_FoxproHead.dwReserve3 = 0x00;
m_FoxproHead.dwReserve4 = 0x00;
m_FoxproHead.dwReserve5 = 0x00;
_fmemset(&m_FoxproFields.strName,0,10);
m_FoxproFields.byFill = 0;
m_FoxproFields.byStyle = (BYTE)'C';
m_FoxproFields.dwDataAddress = 0;
m_FoxproFields.byLength = 0;
m_FoxproFields.byDotCount = 0;
m_FoxproFields.wReserve1 = 0;
m_FoxproFields.byFlags = 0;
_fmemset(&m_FoxproFields.wsReserve,0,11);
if(!(m_pFile=fopen(pStr,"w+b")))
{
return(FALSE);
}
fwrite(&m_FoxproHead ,sizeof(FOXPRO_HEAD) ,1,m_pFile);
fwrite(&m_FoxproFields,sizeof(FOXPRO_FIELDS),nFields,m_pFile);
fwrite("\x0D",1,1,m_pFile);
fwrite("\x1A",1,1,m_pFile);
//File Name
_fstrcpy(m_strFile,pStr);
//Malloc RecordBuf
m_pRecordBuf = new BYTE[this->GetRecordLength()+1024];
_fmemset(m_pRecordBuf,0,this->GetRecordLength()+1024);
//Malloc Foxpro_Fields
m_pFoxproFields = new FOXPRO_FIELDS[1+this->GetFieldsCount()];
_fmemset(m_pFoxproFields,0,sizeof(FOXPRO_FIELDS)*(1+this->GetFieldsCount()));
//Fill FoxproFields
::fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
nFields = this->GetFieldsCount();
for(int i=0;i<nFields;i++)
{
::fread(&m_pFoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
}
return(TRUE);
}
BOOL CFoxpro::OpenFoxpro(const char* pStr)
{
if (m_pFile != NULL)
{
return(FALSE);
}
CoFoxpro();
if (!(m_pFile = fopen(pStr,"r+b")))
{
return(FALSE);
}
long lFileLength = filelength(fileno(m_pFile));
if(lFileLength<1L*sizeof(FOXPRO_HEAD)+2)
{
::fclose(m_pFile);
m_pFile=NULL;
return(FALSE);
}
rewind(m_pFile);
fread(&m_FoxproHead ,sizeof(FOXPRO_HEAD) ,1,m_pFile);
//Flags
if(m_FoxproHead.byFlags!=0x03 && m_FoxproHead.byFlags!=0xF5)
{
::fclose(m_pFile);
m_pFile=NULL;
return(FALSE);
}
BYTE nCh=0;
fseek(m_pFile,-1,SEEK_END);
fread(&nCh,1,1,m_pFile);
// End Is 0x1A?
if(nCh!=0x1A)
{
fclose(m_pFile);
m_pFile=NULL;
return(FALSE);
}
//---------------------------
//File Name
_fstrcpy(m_strFile,pStr);
//Malloc Record Buffer
m_pRecordBuf = new BYTE[this->GetRecordLength()+1024];
if (m_pRecordBuf == NULL)
{
fclose(m_pFile);
m_pFile=NULL;
return (FALSE);
}
_fmemset(m_pRecordBuf,0,this->GetRecordLength()+1024);
//Malloc Foxpro_Fields
m_pFoxproFields = new FOXPRO_FIELDS[1+this->GetFieldsCount()];
if (m_pFoxproFields == NULL)
{
delete m_pRecordBuf;
m_pRecordBuf = NULL;
fclose(m_pFile);
m_pFile=NULL;
return (FALSE);
}
_fmemset(m_pFoxproFields,0,sizeof(FOXPRO_FIELDS)*(1+this->GetFieldsCount()));
//Fill FoxproFields
::fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
int nFields = this->GetFieldsCount();
for(int i=0;i<nFields;i++)
{
::fread(&m_pFoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
}
return(TRUE);
}
BOOL CFoxpro::CloseFoxpro()
{
if (NULL != m_pFile)
{
fclose(m_pFile);
}
m_pFile = NULL;
DELETE(m_pRecordBuf);
DELETE(m_pFoxproFields);
return(TRUE);
}
BOOL CFoxpro::AddFields(const char* strName,BYTE byStyle,int nLength,int nDot)
{
if(!m_pFile) return(FALSE);
if(byStyle=='C') {nDot = 0;}
else if(byStyle=='N') {}
else if(byStyle=='D') {nLength = 8;nDot = 0;}
else if(byStyle=='L') {nLength=1;nDot =0;}
else if(byStyle=='M') {nLength = 10;nDot = 0;}
else {return(0);}
_fmemset(&m_FoxproFields,0,sizeof(FOXPRO_FIELDS));
//----------------------------------------------------
if(byStyle=='M')
{
m_FoxproHead.byFlags=0xF5;
fseek(m_pFile,0,SEEK_SET);
fwrite(&m_FoxproHead ,sizeof(FOXPRO_HEAD) ,1,m_pFile);
}
//Calc Fields
int nFields=(m_FoxproHead.wRecordAddress-sizeof(FOXPRO_HEAD))/sizeof(FOXPRO_FIELDS);
//Modify Head
date mDate;
::getdate(&mDate);
m_FoxproHead.byDay = (BYTE)mDate.da_day;
m_FoxproHead.byMonth = (BYTE)mDate.da_mon;
m_FoxproHead.byYear = (BYTE)(WORD)(mDate.da_year-1900);
m_FoxproHead.wRecordAddress+= sizeof(FOXPRO_FIELDS);
m_FoxproHead.wRecordLength += (WORD)nLength;
//Write Head Again
fseek(m_pFile,0,SEEK_SET);
fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
//
int nFieldsAddress = 1;
fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
for(int i=0;i<nFields;i++)
{
::fread(&m_FoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
nFieldsAddress+=m_FoxproFields.byLength;
}
//Fill in Field
if (::_fstrlen(strName) < 10)
{
::_fstrcpy((char*)&m_FoxproFields.strName,(char*)strName);
}
else
{
::_fmemcpy((BYTE*)&m_FoxproFields.strName, (char*)strName, 10);
}
m_FoxproFields.byStyle = (BYTE)byStyle;
m_FoxproFields.dwDataAddress = nFieldsAddress;
m_FoxproFields.byLength = (BYTE)nLength;
m_FoxproFields.byDotCount = nDot;
//Write Field
::fseek(m_pFile,sizeof(FOXPRO_HEAD)+nFields*sizeof(FOXPRO_FIELDS),SEEK_SET);
::fwrite(&m_FoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
//Write End
fwrite("\x0D",1,1,m_pFile);
fwrite("\x1A",1,1,m_pFile);
//Malloc Record BUf
DELETE(m_pRecordBuf);
m_pRecordBuf = new BYTE[this->GetRecordLength()+1024];
_fmemset(m_pRecordBuf,' ',this->GetRecordLength()+1024);
m_pRecordBuf[GetRecordLength()]='\0';
//Reset Record Point
m_lRecordPoint=0;
//Malloc Foxpro Fields
DELETE(m_pFoxproFields);
m_pFoxproFields = new FOXPRO_FIELDS[1+this->GetFieldsCount()];
_fmemset(m_pFoxproFields,0,sizeof(FOXPRO_FIELDS)*(1+this->GetFieldsCount()));
//Fill FoxproFields
::fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
nFields = this->GetFieldsCount();
for(i=0;i<nFields;i++)
{
::fread(&m_pFoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
}
return(TRUE);
}
char* CFoxpro::TrimLeft(char *pStr)
{
//if(_fstrlen(pStr)>=256) return(pStr);
while(pStr[0]==' '||pStr[0]=='\x9')
{
int nLength=_fstrlen(pStr);
if(!nLength) break;
if(nLength) _fmemcpy(&pStr[0],&pStr[1],nLength);
}
return(pStr);
}
char* CFoxpro::TrimRight(char *pStr)
{
//if(_fstrlen(pStr)>=256) return(pStr);
int nLength=_fstrlen(pStr);
if(!nLength) return(pStr);
for(int i=nLength-1;i>=0;i--)
{
if(pStr!=' '&&pStr!='\x9') break;
pStr='\0';
}
return(pStr);
}
FOXPRO_FIELDS* CFoxpro::GetFoxproFields(int nIndex)
{
::_fmemset(&m_FoxproFields,0,sizeof(FOXPRO_FIELDS));
//----------------------------------------------------
//Calc Fields
int nFields=this->GetFieldsCount();
if(nIndex>=nFields||nFields<0) return(&m_FoxproFields);
return(&m_pFoxproFields[nIndex]);
}
BOOL CFoxpro::GetFieldsString(int nIndex,char* pStr)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='C') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(pStr,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
if(m_FoxproFields.byLength)
{
pStr[m_FoxproFields.byLength] = '\0';
}
this->TrimRight(pStr);
return(TRUE);
}
float CFoxpro::GetFieldsNumber(int nIndex)
{
char buf[120];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='N') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(buf,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
buf[m_FoxproFields.byLength] = '\0';
return(atof(buf));
}
BOOL CFoxpro::GetFieldsDate(int nIndex,int* pnYear,int* pnMonth,int* pnDay)
{
char buf1[20],buf2[20];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='D') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(buf1,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
buf1[m_FoxproFields.byLength] = '\0';
buf2[0]=buf1[0];
buf2[1]=buf1[1];
buf2[2]=buf1[2];
buf2[3]=buf1[3];
buf2[4]='\0';
*pnYear =(int)(atof(buf2)+0.5f);
buf2[0]=buf1[4];
buf2[1]=buf1[5];
buf2[2]='\0';
*pnMonth =(int)(atof(buf2)+0.5f);
buf2[0]=buf1[6];
buf2[1]=buf1[7];
buf2[2]='\0';
*pnDay =(int)(atof(buf2)+0.5f);
return(TRUE);
}
BOOL CFoxpro::GetFieldsLogic(int nIndex)
{
char buf[10];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='L') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(buf,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
buf[m_FoxproFields.byLength] = '\0';
if(buf[0]=='T') return(TRUE);
return(FALSE);
}
BOOL CFoxpro::GetFieldsMemo(int /*nIndex*/,char* /*pStr*/)
{
return(TRUE);
}
BOOL CFoxpro::SetFieldsString(int nIndex, const char* pStr)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='C') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
int nLength1 = _fstrlen(pStr);
int nLength2 = (WORD)m_FoxproFields.byLength;
for(int i=0;i<nLength2;i++)
{
if(i>=nLength1)
{
m_pRecordBuf[nAddress+i]=' ';
}
else
{
m_pRecordBuf[nAddress+i]=*(pStr+i);
}
}
//::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
//::fseek(m_pFile,m_lRecordPoint*GetRecordLength(),SEEK_CUR);
//::fseek(m_pFile,nAddress,SEEK_CUR);
return(TRUE);
}
BOOL CFoxpro::SetFieldsNumber(int nIndex,float fValue)
{
char buf[100];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='N') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
if(m_FoxproFields.byDotCount==0) sprintf(buf,"%.0f",fValue);
else if(m_FoxproFields.byDotCount==1) sprintf(buf,"%.1f",fValue);
else if(m_FoxproFields.byDotCount==2) sprintf(buf,"%.2f",fValue);
else if(m_FoxproFields.byDotCount==3) sprintf(buf,"%.3f",fValue);
else if(m_FoxproFields.byDotCount==4) sprintf(buf,"%.4f",fValue);
else if(m_FoxproFields.byDotCount==5) sprintf(buf,"%.5f",fValue);
else sprintf(buf,"%.6f",fValue);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
int nLength1 = _fstrlen(buf);
int nLength2 = (WORD)m_FoxproFields.byLength;
for(int i=0;i<nLength2;i++)
{
if(i>=nLength1)
{
m_pRecordBuf[nAddress+i]=' ';
}
else
{
m_pRecordBuf[nAddress+i]=buf;
}
}
return(TRUE);
}
BOOL CFoxpro::SetFieldsDate(int nIndex,int nYear,int nMonth,int nDay)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='D') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
//sprintf((char *)&m_pRecordBuf[nAddress],"%4d%2d%2d%",nYear,nMonth,nDay);
//m_pRecordBuf[nAddress+8]=' ';
char buf[40];
sprintf(buf,"%4d%2d%2d",nYear,nMonth,nDay);
for(int i=0;i<8;i++)
{
m_pRecordBuf[nAddress+i] = buf;
}
return(TRUE);
}
BOOL CFoxpro::SetFieldsLogic(int nIndex,BOOL bLogic)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='L') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
int nAddress = (WORD)m_FoxproFields.dwDataAddress;
if(bLogic) m_pRecordBuf[nAddress]='T';
else m_pRecordBuf[nAddress]='F';
return(TRUE);
}
BOOL CFoxpro::SetFieldsMemo(int nIndex, const char* /*pStr*/)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);
m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='M') return(FALSE);
if(!m_pRecordBuf) return(FALSE);
//int nAddress = (WORD)m_FoxproFields.dwDataAddress;
//((DWORD *)&m_pRecordBuf[nAddress])=(BYTE)chLogic;
return(TRUE);
}
BOOL CFoxpro::Seek(long nIndex,int nMode)
{
if(!m_pFile) return(FALSE);
if(nMode==SEEK_SET) m_lRecordPoint = nIndex;
else if(nMode==SEEK_CUR) m_lRecordPoint+= nIndex;
else if(nMode==SEEK_END)
{
m_lRecordPoint = GetRecordCount();
m_lRecordPoint+= nIndex;
}
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,m_lRecordPoint*GetRecordLength(),SEEK_CUR);
if(m_lRecordPoint<this->GetRecordCount())
{
::fread(m_pRecordBuf,this->GetRecordLength(),1,m_pFile);
m_pRecordBuf[GetRecordLength()]='\0';
::fseek(m_pFile,0L-GetRecordLength(),SEEK_CUR);
}
return(TRUE);
}
BOOL CFoxpro::Delete()
{
if(!m_pFile) return(FALSE);
if(!m_pRecordBuf) return(FALSE);
if(m_lRecordPoint>=this->GetRecordCount()) return(FALSE);
this->Edit();
m_pRecordBuf[0]='*';
this->Update();
return(TRUE);
}
BOOL CFoxpro::Zap()
{
if(!m_pFile) return(FALSE);
if(!this->GetRecordCount()) return(TRUE);
long lRecordCount = this->GetRecordCount();
long lRecordLength = this->GetRecordLength();
long lLocation=0;
for(long i=0;i<lRecordCount;i++)
{
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,i*lRecordLength,SEEK_CUR);
::fread(m_pRecordBuf,lRecordLength,1,m_pFile);
if(m_pRecordBuf[0] != '*')
{
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,lLocation*lRecordLength,SEEK_CUR);
::fwrite(m_pRecordBuf,lRecordLength,1,m_pFile);
lLocation++;
}
}
this->m_FoxproHead.dwRecordCount=lLocation;
::fseek(m_pFile,0,SEEK_SET);
::fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
long lFileLength = sizeof(FOXPRO_HEAD)+
1L*sizeof(FOXPRO_FIELDS)*this->GetFieldsCount()+
1L+
1L*this->m_FoxproHead.dwRecordCount*lRecordLength;
::chsize(fileno(m_pFile),lFileLength);
::fseek(m_pFile,0,SEEK_END);
::fwrite("\x1A",1,1,m_pFile);
m_lRecordPoint=0L;
this->Seek(m_lRecordPoint,SEEK_SET);
this->Edit();
return(TRUE);
}
BOOL CFoxpro::DeleteEx()
{
if(!m_pFile) return(FALSE);
if(m_lRecordPoint>=this->GetRecordCount()) return(FALSE);
long lRecordCount = this->GetRecordCount();
long lRecordLength = this->GetRecordLength();
for(long i=m_lRecordPoint+1;i<lRecordCount;i++)
{
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,i*lRecordLength,SEEK_CUR);
::fread(m_pRecordBuf,lRecordLength,1,m_pFile);
::fseek(m_pFile,-2L*lRecordLength,SEEK_CUR);
::fwrite(m_pRecordBuf,lRecordLength,1,m_pFile);
}
this->m_FoxproHead.dwRecordCount-=1;
::fseek(m_pFile,0,SEEK_SET);
::fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
long lFileLength = sizeof(FOXPRO_HEAD)+
1L*sizeof(FOXPRO_FIELDS)*this->GetFieldsCount()+
1L+
1L*this->m_FoxproHead.dwRecordCount*lRecordLength;
::chsize(fileno(m_pFile),lFileLength);
::fseek(m_pFile,0,SEEK_END);
::fwrite("\x1A",1,1,m_pFile);
if(m_lRecordPoint && m_lRecordPoint==this->GetRecordCount())
{
m_lRecordPoint-=1;
}
//------------------------------------------------------------
this->Seek(m_lRecordPoint,SEEK_SET);
this->Edit();
return(TRUE);
}
BOOL CFoxpro::Insert()
{
return(TRUE);
}
BOOL CFoxpro::AddNew()
{
if (NULL == m_pFile)
{
return(FALSE);
}
m_lRecordPoint = this->GetRecordCount();
_fmemset(m_pRecordBuf,' ',this->GetRecordLength()+1024);
this->Seek(0,SEEK_END);
m_bNew = TRUE;
return(TRUE);
}
BOOL CFoxpro::Edit()
{
if(NULL == m_pFile)
{
return(FALSE);
}
if (m_bNew)
{
return(TRUE);
}
this->Seek(m_lRecordPoint,SEEK_SET);
::fread(m_pRecordBuf,this->GetRecordLength(),1,m_pFile);
m_pRecordBuf[GetRecordLength()]='\0';
return(TRUE);
}
BOOL CFoxpro::Update()
{
if (NULL == m_pFile) return(FALSE);
if(m_bNew)
{
m_lRecordPoint = this->GetRecordCount();
}
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,m_lRecordPoint*GetRecordLength(),SEEK_CUR);
::fwrite(m_pRecordBuf,this->GetRecordLength(),1,m_pFile);
if (m_bNew)
{
//::fseek(m_pFile, 0, SEEK_END);
fwrite("\x1A",1,1,m_pFile);
rewind(m_pFile);
m_FoxproHead.dwRecordCount += 1;
::fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
}
m_bNew = FALSE;
return(TRUE);
}
BOOL CFoxpro::IsOpen()
{
return(((m_pFile)?TRUE:FALSE));
}
BOOL CFoxpro::Flush()
{
if (NULL == m_pFile)
{
return(FALSE);
}
::fflush(m_pFile);
int duphandle = ::dup(fileno(m_pFile));
::close(duphandle);
return(TRUE);
}
|
|
2018-7-14 12:45 |
|
|
firstsail
高级用户
积分 668
发帖 295
注册 2005-7-26 来自 广东深圳
状态 离线
|
『第
5 楼』:
数据库类CFoxpro的应用
//数据库类CFoxpro的应用
//建立一新数据库
BOOL BuildArmatureDbf(char* pStr)
{
char buf[50];
//分配一个数据库对象
CFoxpro mFoxpro;
//建立并打开新数据库
if (!mFoxpro.CreateFoxpro(pStr))
{
return(FALSE);
}
//字符类型,最大24字节,0小数点(字符当然没有小数点的概念!)
mFoxpro.AddFields("ModelName" ,'C',24,0);
//日期类型,10字节长度(必须是这个值),0小数点(日期当然没有小数点的概念!)
mFoxpro.AddFields("Date" ,'D',10,0);
//数字类型,9字节长度(必须是这个值),0小数点
mFoxpro.AddFields("Line" ,'N', 9,0);
//数字类型,2字节长度(必须是这个值),0小数点
mFoxpro.AddFields("Bar" ,'N', 2,0);
//数字类型,5字节长度(必须是这个值),3小数点
mFoxpro.AddFields("Hipot1" ,'N',5,3);
//数字类型,5字节长度(必须是这个值),3小数点
mFoxpro.AddFields("Hipot2" ,'N',5,3);
//数字类型,5字节长度(必须是这个值),3小数点
mFoxpro.AddFields("Hipot3" ,'N',5,3);
//数字类型,4字节长度(必须是这个值),0小数点
mFoxpro.AddFields("Insulation",'N',4,0);
//关闭数据库
mFoxpro.CloseFoxpro();
return(TRUE);
}
//写纪录
BOOL WriteMapFile()
{
CFoxpro mFoxpro;
if (!mFoxpro.OpenFoxpro("MyName.Dbf"))
{
return(FALSE);
}
//建立新纪录
mFoxpro.AddNew();
mFoxpro.Edit();
//写入型号
mFoxpro.SetFieldsString(0, "COIN-003");
//写入日期
mFoxpro.SetFieldsDate(1,2004,10,30);
//写入一整数
mFoxpro.SetFieldsNumber(2,3275);
//写入一整数
mFoxpro.SetFieldsNumber(3,24);
//更新新效
mFoxpro.Update();
//关闭文件
mFoxpro.Flush();
mFoxpro.CloseFoxpro();
return(TRUE);
}
//读纪录
BOOL ReadMapFile()
{
CFoxpro mFoxpro;
if (!mFoxpro.OpenFoxpro("MyName.Dbf"))
{
return(FALSE);
}
//定位到第8条纪录
mFoxpro.Seek(7, SEEK_SET);
//读字符串
char buf[256];
mFoxpro.GetFieldsString(0, buf);
//读日期
int nYear, nMonth, nDay;
mFoxpro.GetFieldsDate(1, &nYear, &nMonth, &nDay);
//读数
float fData = mFoxpro.GetFieldsNumber(2);
//关闭文件
mFoxpro.CloseFoxpro();
return(TRUE);
}
|
|
2018-7-14 12:47 |
|
|
firstsail
高级用户
积分 668
发帖 295
注册 2005-7-26 来自 广东深圳
状态 离线
|
『第
6 楼』:
上面代码在Borland C++3.1版本中编译通过
如果要移值到Visual C++6.0,只需稍微修改就可以了!
|
|
2018-7-14 17:51 |
|
|