另一个关于文件的问题 就怎么样才可以以数据库的形式把 数据 保存到文件中!读取和栓出 添加 怎么样对这个文件进行操作啊!大家就就我啊!我看了很就还是没有弄清楚…… 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 struct _DBRECORD{ int NO; unsigned char Name[11]; int Type; long Value; unsigned char Desc[51]; int DBNO; int Addr;};/////////////////////////////////////////////////////////////////////////////// CFileSetclass CFileSet : public CObject{ DECLARE_DYNCREATE(CFileSet)public: CFileSet();// Attributesprotected: CString filename; CFile file;public: // record struct _DBRECORD record; // control message int position; BOOL Status; int length;// field member variablespublic: int NO; CString Name; int Type; long Value; CString Desc; int DBNO; int Addr;// Navigation Operationspublic: void Move(long nRows); void MoveFirst(); void MoveLast(); void MovePrev(); void MoveNext(); BOOL IsBOF(); BOOL IsEOF();// Recordset Update Operationspublic: BOOL AddNew(); BOOL Delete(); BOOL Update();// Operationsprotected: BOOL Read(); BOOL Write();// Implementationpublic: CString temppath; BOOL Check(); void UpdateData(BOOL bUpdate); BOOL Open(CString m_strFile); void Close(); virtual ~CFileSet(); // Generated message map functions //{{AFX_MSG(CFileSet) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP();};int recordsize = sizeof(_DBRECORD);IMPLEMENT_DYNCREATE(CFileSet, CObject)CFileSet::CFileSet(){ filename = _T(""); Status = FALSE; position = 0; length = 0; unsigned char buff[1024]; memset(buff,0,1024); ::GetTempPath(1024,(char *)&buff); temppath.Format("%s",buff); memset(&record,0,sizeof(record)); NO = 0; Name=_T(""); Type = 0; Value = 0; Desc = _T(""); DBNO = 0; Addr = 0;}CFileSet::~CFileSet(){ Close();}BEGIN_MESSAGE_MAP(CFileSet, CObject) //{{AFX_MSG_MAP(CFileSet) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CFileSet message handlersBOOL CFileSet::Open(CString m_strFile){ filename = m_strFile; Status = file.Open(filename,CFile::modeReadWrite,NULL); if (Status) { length = (file.GetLength())/recordsize; //file.SeekToBegin(); if (length>0) Read(); } return Status;}void CFileSet::Close(){ if (Status) { file.Close(); Status = FALSE; position = 0; length = 0; }}void CFileSet::Move(long nRows){ ASSERT(Status == TRUE); int mv = nRows; int sz = position+mv; if (sz<0) { mv=-position; } else if (sz>=(length-1)) { mv=length-1-position; } if (file.Seek(recordsize*mv,CFile::current) <0 ) { return; } position += mv; Read();}void CFileSet::MoveFirst(){ ASSERT(Status == TRUE); file.SeekToBegin(); position = 0; Read();}void CFileSet::MoveLast(){ ASSERT(Status == TRUE); // as we known length if larger than 0 if (length == 0 || length ==1) { MoveFirst(); } else { file.Seek(-recordsize,CFile::end); position = length-1; } Read();}void CFileSet::MovePrev(){ ASSERT(Status == TRUE); if ((position == 0) || (length == 0)) { return; } if (file.Seek(-recordsize,CFile::current) <0 ) { return; } position--; Read();}void CFileSet::MoveNext(){ ASSERT(Status == TRUE); if ((length == 0) || (position == (length)) ) { return; } if (file.Seek(recordsize,CFile::current) <0 ) { return; } position++; Read();}BOOL CFileSet::IsBOF(){ ASSERT(Status == TRUE); return(position == 0);}BOOL CFileSet::IsEOF(){ ASSERT(Status == TRUE); return(position == length);}BOOL CFileSet::AddNew(){ ASSERT(Status == TRUE); file.SeekToEnd(); if (Write()) { length++; position = length -1; } return TRUE;}BOOL CFileSet::Delete(){ ASSERT(Status == TRUE); if ((length == 0) || (position == length)) { return FALSE; } CFile temp; CString tempfile; // delete tempfile.Format("%sd001.tmp",temppath); temp.Open(tempfile,CFile::modeCreate|CFile::modeReadWrite,NULL); unsigned char buff[1024]; memset(buff,0,1024); DWORD pos = file.GetPosition(); file.Seek(recordsize,CFile::current); int sz=0; while((sz=file.Read(&buff,1024))>0) { temp.Write(buff,sz); temp.Flush(); } file.Seek(pos,CFile::begin); temp.SeekToBegin(); while((sz=temp.Read(&buff,1024))>0) { file.Write(buff,sz); } temp.Close(); file.SetLength(file.GetLength()-recordsize); CFile::Remove(tempfile); // reload file.Flush(); length = file.GetLength()/recordsize; MoveFirst(); Move(position); return TRUE;}BOOL CFileSet::Update(){ ASSERT(Status == TRUE); if ((length == 0) || (position == length)) { return FALSE; } Write(); return TRUE;}BOOL CFileSet::Read(){ file.Read(&record,sizeof(record)); file.Seek(-recordsize,CFile::current); UpdateData(FALSE); return TRUE;}BOOL CFileSet::Write(){ UpdateData(TRUE); file.Write(&record,sizeof(record)); file.Flush(); file.Seek(-recordsize,CFile::current); return TRUE;}void CFileSet::UpdateData(BOOL bUpdate){ if (bUpdate) { record.NO = NO; // position+1; strcpy((char *)record.Name,LPCTSTR(Name)); record.Name[10] = 0; record.Type = Type; record.Value = Value; strcpy((char *)record.Desc,LPCTSTR(Desc)); record.Desc[50] = 0; record.DBNO = DBNO; record.Addr = Addr; } else { NO = record.NO; Name.Format("%s",record.Name); Type = record.Type; Value = record.Value; Desc.Format("%s",record.Desc); DBNO = record.DBNO; Addr = record.Addr; }}BOOL CFileSet::Check(){ return TRUE;}这个类可以实现添加删除等操作,希望对你有帮助! 如何把一个文件添加到一个.exe文件中 在数据库与VC6.0的链接,做窗口时,只会做每个表的窗口,不知道怎么做主窗口然后进入到其它窗口 c++中怎么封装常用类,利于多个工程调用? 自己用代码添加了一个控件,怎么给这个控件添加单击消息响应呀? 聊天软件 多人聊天功能 操作系统补丁是如何打的——如何覆盖正在使用的文件? ListCtrl的问题? 我在一个对话框中通过单击一个按钮弹出一个无模式对话框,如何判断已打开该窗口,代码该怎样写 内存泄露问题(高手进) 我在2000下面写ASP组件 为什么错?,不解 在线等........ 非MFC下,如何清空一个ListBox里的内容?
{
int NO;
unsigned char Name[11];
int Type;
long Value;
unsigned char Desc[51];
int DBNO;
int Addr;
};/////////////////////////////////////////////////////////////////////////////
// CFileSetclass CFileSet : public CObject
{
DECLARE_DYNCREATE(CFileSet)
public:
CFileSet();// Attributes
protected:
CString filename;
CFile file;public:
// record struct
_DBRECORD record; // control message
int position;
BOOL Status;
int length;// field member variables
public:
int NO;
CString Name;
int Type;
long Value;
CString Desc;
int DBNO;
int Addr;// Navigation Operations
public:
void Move(long nRows);
void MoveFirst();
void MoveLast();
void MovePrev();
void MoveNext(); BOOL IsBOF();
BOOL IsEOF();// Recordset Update Operations
public:
BOOL AddNew();
BOOL Delete();
BOOL Update();// Operations
protected:
BOOL Read();
BOOL Write();// Implementation
public:
CString temppath;
BOOL Check();
void UpdateData(BOOL bUpdate);
BOOL Open(CString m_strFile);
void Close(); virtual ~CFileSet(); // Generated message map functions
//{{AFX_MSG(CFileSet)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG DECLARE_MESSAGE_MAP();
};
int recordsize = sizeof(_DBRECORD);IMPLEMENT_DYNCREATE(CFileSet, CObject)CFileSet::CFileSet()
{
filename = _T("");
Status = FALSE;
position = 0;
length = 0; unsigned char buff[1024];
memset(buff,0,1024);
::GetTempPath(1024,(char *)&buff);
temppath.Format("%s",buff); memset(&record,0,sizeof(record)); NO = 0;
Name=_T("");
Type = 0;
Value = 0;
Desc = _T("");
DBNO = 0;
Addr = 0;
}CFileSet::~CFileSet()
{
Close();
}BEGIN_MESSAGE_MAP(CFileSet, CObject)
//{{AFX_MSG_MAP(CFileSet)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CFileSet message handlersBOOL CFileSet::Open(CString m_strFile)
{
filename = m_strFile;
Status = file.Open(filename,CFile::modeReadWrite,NULL);
if (Status)
{
length = (file.GetLength())/recordsize;
//file.SeekToBegin();
if (length>0)
Read();
}
return Status;
}void CFileSet::Close()
{
if (Status)
{
file.Close();
Status = FALSE;
position = 0;
length = 0;
}
}void CFileSet::Move(long nRows)
{
ASSERT(Status == TRUE); int mv = nRows;
int sz = position+mv;
if (sz<0)
{
mv=-position;
}
else if (sz>=(length-1))
{
mv=length-1-position;
} if (file.Seek(recordsize*mv,CFile::current) <0 )
{
return;
}
position += mv;
Read();
}void CFileSet::MoveFirst()
{
ASSERT(Status == TRUE);
file.SeekToBegin();
position = 0;
Read();
}void CFileSet::MoveLast()
{
ASSERT(Status == TRUE); // as we known length if larger than 0
if (length == 0 || length ==1)
{
MoveFirst();
}
else
{
file.Seek(-recordsize,CFile::end);
position = length-1;
}
Read();
}void CFileSet::MovePrev()
{
ASSERT(Status == TRUE);
if ((position == 0) || (length == 0))
{
return;
} if (file.Seek(-recordsize,CFile::current) <0 )
{
return;
}
position--;
Read();
}void CFileSet::MoveNext()
{
ASSERT(Status == TRUE); if ((length == 0) || (position == (length)) )
{
return;
}
if (file.Seek(recordsize,CFile::current) <0 )
{
return;
} position++;
Read();
}BOOL CFileSet::IsBOF()
{
ASSERT(Status == TRUE);
return(position == 0);
}BOOL CFileSet::IsEOF()
{
ASSERT(Status == TRUE);
return(position == length);
}BOOL CFileSet::AddNew()
{
ASSERT(Status == TRUE);
file.SeekToEnd();
if (Write())
{
length++;
position = length -1;
}
return TRUE;
}BOOL CFileSet::Delete()
{
ASSERT(Status == TRUE); if ((length == 0) || (position == length))
{
return FALSE;
} CFile temp;
CString tempfile; // delete
tempfile.Format("%sd001.tmp",temppath);
temp.Open(tempfile,CFile::modeCreate|CFile::modeReadWrite,NULL); unsigned char buff[1024];
memset(buff,0,1024);
DWORD pos = file.GetPosition();
file.Seek(recordsize,CFile::current);
int sz=0;
while((sz=file.Read(&buff,1024))>0)
{
temp.Write(buff,sz);
temp.Flush();
}
file.Seek(pos,CFile::begin);
temp.SeekToBegin();
while((sz=temp.Read(&buff,1024))>0)
{
file.Write(buff,sz);
}
temp.Close();
file.SetLength(file.GetLength()-recordsize);
CFile::Remove(tempfile); // reload
file.Flush();
length = file.GetLength()/recordsize;
MoveFirst();
Move(position); return TRUE;
}BOOL CFileSet::Update()
{
ASSERT(Status == TRUE);
if ((length == 0) || (position == length))
{
return FALSE;
}
Write();
return TRUE;
}BOOL CFileSet::Read()
{
file.Read(&record,sizeof(record));
file.Seek(-recordsize,CFile::current);
UpdateData(FALSE);
return TRUE;
}BOOL CFileSet::Write()
{
UpdateData(TRUE);
file.Write(&record,sizeof(record));
file.Flush();
file.Seek(-recordsize,CFile::current);
return TRUE;
}void CFileSet::UpdateData(BOOL bUpdate)
{
if (bUpdate)
{
record.NO = NO; // position+1;
strcpy((char *)record.Name,LPCTSTR(Name));
record.Name[10] = 0;
record.Type = Type;
record.Value = Value;
strcpy((char *)record.Desc,LPCTSTR(Desc));
record.Desc[50] = 0;
record.DBNO = DBNO;
record.Addr = Addr;
}
else
{
NO = record.NO;
Name.Format("%s",record.Name);
Type = record.Type;
Value = record.Value;
Desc.Format("%s",record.Desc);
DBNO = record.DBNO;
Addr = record.Addr;
}
}BOOL CFileSet::Check()
{
return TRUE;
}
这个类可以实现添加删除等操作,希望对你有帮助!