就怎么样才可以以数据库的形式把 数据 保存到文件中!
读取和栓出 添加 怎么样对这个文件进行操作啊!大家就就我啊!
我看了很就还是没有弄清楚……

解决方案 »

  1.   

    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();// 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;
    }
    这个类可以实现添加删除等操作,希望对你有帮助!