你参考一下这个
在TC2.0(具体我是在Tornado II开发环境使用C编译器,Tornado II是用来开发实时操作系统VxWork的工具)使用.我想将WinNT下的数据库(*.DB)转换为二进制文件数据库,以便能在VxWorks中使用。
我首先将Paradox数据库文件放到Table1中,再把数据库的信息依次写到文件中。(代码如下:)
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int __fastcall TForm1::GetDBFieldNum(void)
{ //获得字段数目
FieldNum=DBGrid1->Columns->Count;
return FieldNum;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::GetRecordNum(void)
{ //获取记录数目
RecordNum=Table1->RecordCount;
return RecordNum;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::GetFieldInf(void)
{ //获取字段的信息(字段名、字段类型、字段长度)
RecordLength=0;
for (int i=0;i<FieldNum;i++)
{
FieName
=DBGrid1->Columns->Items->FieldName;
FieType=Table1->Fields->DataType;
FieLength=Table1->Fields->DisplayWidth;
RecordLength=RecordLength+FieLength;
}
return RecordLength;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::WriteFileHead()
{ //写入字段数、记录数、记录长度
fwrite(&FieldNum,sizeof(int),1,fp);
fwrite(&RecordNum,sizeof(int),1,fp);
fwrite(&RecordLength,sizeof(int),1,fp);
for (int i=0;i<FieldNum;i++)
{ //写入字段信息(字段名、字段类型、字段长度)
fwrite(&FieName,80,1,fp);
fwrite(&FieType,20,1,fp);
fwrite(&FieLength,10,1,fp);
}
}
//---------------------------------------------------------------------------
int __fastcall TForm1::ReadFileHead()
{ //读取文件头信息(字段数、记录数、记录长度)和记录的结构信息
rewind(fp);
fread(&FieldNum,sizeof(int),1,fp);
fread(&RecordNum,sizeof(int),1,fp);
fread(&RecordLength,sizeof(int),1,fp);
for (int i=0;i<FieldNum;i++)
{
fread(&FieName1,80,1,fp);
fread(&FieType,20,1,fp);
fread(&FieLength,10,1,fp);
}
SeekNum=ftell(fp);
return SeekNum;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::WriteRecord()
{
AnsiString Record;
AnsiString TmpFie;
Table1->First();
while (!Table1->Eof)
{
Record="";
for (int i=0;i<FieldNum;i++)
{
TmpFie="";
TmpFie=Table1->FieldByName(FieName)->AsString;
fwrite(&TmpFie,FieLength,1,fp);
}
Table1->Next();
}
return ((ftell(fp)-SeekNum)/ RecordLength);
}
//----------------------------------------------------------------------------
int __fastcall TForm1::ReadRecord()
{
fclose(fp);
fp=fopen(SaveDialog1->FileName.c_str(),"rb+");
fseek(fp,SeekNum,0);
int Counter;
AnsiString FieStr1[50];
while (!feof(fp))
{
for (int i=0;i<FieldNum;i++)
{
if(fread(&FieStr1,FieLength,1,fp))
StringGrid1->Cells[i+1][Counter+1]=FieStr1;
else
break;
}
Counter=Counter+1;
}
return (ftell(fp)-SeekNum)/RecordLength;
}
//----------------------------------------------------------------------------
void __fastcall TForm1::RefreshGrid()
{
StringGrid1->ColCount=FieldNum+1;
StringGrid1->RowCount=RecordNum+2;
StringGrid1->FixedRows=1;
StringGrid1->FixedCols=1;
StringGrid1->ColWidths[0]=12;
for (int i=0;i<FieldNum;i++)
{
StringGrid1->Cells[i+1][0]=FieName1;
StringGrid1->ColWidths[i+1]=40+FieLength*2;
}
}
//----------------------------------------------------------------------------
int __fastcall TForm1::First()
{
fseek(fp,SeekNum,0);
return 1;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Last()
{
fseek(fp,-0,2);
return (ftell(fp)-SeekNum)/RecordLength;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Next()
{
if(!fseek(fp,RecordLength,1)) return -1;
else
return ((ftell(fp)-SeekNum)/RecordLength);
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Append()
{
fseek(fp,sizeof(int),0);
RecordNum=RecordNum+1;
fwrite(&RecordNum,sizeof(int),1,fp);
fseek(fp,-0,2);
for (int i=0;i<FieldNum;i++)
{
fwrite(&FieStr,FieLength,1,fp);
}
return (ftell(fp)-SeekNum)/RecordLength;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Insert()
{
}
//---------------------------------------------------------------------------
int __fastcall TForm1:elete()
{
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Edit()
{
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Local()
{
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Find()
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender)
{
OpenDialog1->Filter="Paradox 7.0 files(*.DB)|*.DB|Any file(*.*)|*.*";
OpenDialog1->Title="请选择要转换的文件";
OpenDialog1->Execute();
if(FileExists(OpenDialog1->FileName)&&(OpenDialog1->FileName!=""))
{
Table1->Active=false;
Table1->DatabaseName=ExtractFilePath(OpenDialog1->FileName);
Table1->TableName=OpenDialog1->FileName;
Table1->Active=true;
GetDBFieldNum();
GetRecordNum();
GetFieldInf();
N3Click(NULL);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N3Click(TObject *Sender)
{
SaveDialog1->Filter="Binary file (*.Dat)|*.Dat";
SaveDialog1->Title="保存到文件";
if(SaveDialog1->Execute()&&(SaveDialog1->FileName!=""))
{
if ((fp=fopen(SaveDialog1->FileName.c_str(),"wb+"))!=NULL)
WriteFileHead();
WriteRecord();
ReadFileHead();
RefreshGrid();
ReadRecord();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::X1Click(TObject *Sender)
{
Table1->Active=false;
Form1->Close();
}
//---------------------------------------------------------------------------
上面的代码用到的全局变量如下:
FILE *fp;
int FieldNum,RecordNum,RecordLength;
AnsiString FieStr[50];
AnsiString FieName[50],FieName1[50];
AnsiString FieType[50];
int FieLength[50];
int SeekNum;