2010년 5월 7일 금요일

파일을 DB처럼 사용하는법

class CInfo
{
public:
char szName[40];
char szPhone[40];
char szMobile[40];
char szAddress[40];
char szEmail[40];
char szEtc[1024-40*5-7];
char szn[3];


CInfo(){
  memset(szName, 0 ,sizeof(char)*40);
  memset(szPhone, 0 ,sizeof(char)*40);
  memset(szMobile, 0 ,sizeof(char)*40);
  memset(szAddress, 0 ,sizeof(char)*40);
  memset(szEmail, 0 ,sizeof(char)*40);
  memset(szEtc, 0 ,sizeof(char)*1024-40*5-7);
  memset(szn,0,sizeof(szn));
  strncpy(szn,_T("\r\n"),sizeof("\r\n"));
}
};

1.데이터 삽입(Insert)



void CFileDB::InsertInFile(CInfo &Info)
{


m_hFile = CreateFile(_T("c:\\test001.dat"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

DWORD nWrite = 0;

SetFilePointer(m_hFile,0,NULL,FILE_END);

//WriteFile(m_hFile,_T("\n"),sizeof("\n"),&nWrite,NULL);




if(m_hFile != INVALID_HANDLE_VALUE)
{
  WriteFile(m_hFile,&Info,sizeof(Info),&nWrite,NULL);
}

CloseHandle(m_hFile);


}



2.데이터 조회(Select)



void CFileDB::SelectInFile(void)
{

UpdateData(TRUE);


DWORD dwStartTime, dwFinishTime;
CInfo Info;
memset(&Info,0,sizeof(Info));



m_hFile = CreateFile(_T("c:\\test001.dat"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);

int nNumOfData = GetFileSize(m_hFile,NULL)/sizeof(CInfo);

DWORD nRead = 0;

if(m_hFile != INVALID_HANDLE_VALUE)
{
  for(int i = 0 ; i< nNumOfData ; ++i)
  {
   SetFilePointer(m_hFile,sizeof(Info)*i,NULL,FILE_BEGIN);
   ReadFile(m_hFile,&Info,sizeof(Info),&nRead,NULL);

   if(m_strNameSearch.Compare(Info.szName) == 0 )
   {
    // 데이타를 표시한다.
    m_uIndex = i;
    m_strName = Info.szName;
    m_strPhone = Info.szPhone;
    m_strMobile = Info.szMobile;
    m_strAddress = Info.szAddress;
    m_strEmail = Info.szEmail;
    dwFinishTime = GetTickCount();
    m_uTime = 0;

   }
  }
 
}

CloseHandle(m_hFile);

UpdateData(FALSE);

}



3.데이터 수정(Update)



void CFileDB::UpdateInFile(void)
{

UpdateData(TRUE);


DWORD dwStartTime, dwFinishTime;
CInfo Info;
memset(&Info,0,sizeof(Info));

m_hFile = CreateFile(_T("c:\\test001.dat"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);

int nNumOfData = GetFileSize(m_hFile,NULL)/sizeof(CInfo);

DWORD nRead = 0;
DWORD nWrite = 0;

if(m_hFile != INVALID_HANDLE_VALUE)
{
  for(int i = 0 ; i< nNumOfData ; ++i)
  {
   SetFilePointer(m_hFile,sizeof(Info)*i,NULL,FILE_BEGIN);
   ReadFile(m_hFile,&Info,sizeof(Info),&nRead,NULL);

   if(m_strNameSearch.Compare(Info.szName) == 0 )
   {
    // 데이타를 표시한다.
    m_uIndex = i;
    strncpy(Info.szName,m_strName,strlen(m_strName));
    strncpy(Info.szPhone, m_strPhone,strlen(m_strPhone));
    strncpy(Info.szMobile, m_strMobile,strlen(m_strMobile));
    strncpy(Info.szAddress, m_strAddress,strlen(m_strAddress));
    strncpy(Info.szEmail, m_strEmail,strlen(m_strEmail));
    dwFinishTime = GetTickCount();
    m_uTime = 0;

    SetFilePointer(m_hFile,sizeof(Info)*i,NULL,FILE_BEGIN);
    WriteFile(m_hFile,&Info,sizeof(Info),&nWrite,NULL);

    MessageBox(_T("수정이 완료되었습니다."));

   }
  }

}

CloseHandle(m_hFile);

UpdateData(FALSE);

}



4.데이터 삭제(Delete)

void CFileDB::DeleteInFile(void)
{
CInfo Info;
DWORD nRead = 0;
DWORD nWrite = 0;

BOOL bCompare = FALSE;


m_hFile = CreateFile(_T("c:\\test001.dat"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);

int nNumOfData = GetFileSize(m_hFile,NULL)/sizeof(CInfo);

if(m_hFile != INVALID_HANDLE_VALUE)
{
  for(int i = 0 ; i< nNumOfData ; ++i)
  {
   SetFilePointer(m_hFile,sizeof(Info)*i,NULL,FILE_BEGIN);
   ReadFile(m_hFile,&Info,sizeof(Info),&nRead,NULL);

   if(m_strName.Compare(Info.szName) == 0 )
   {
    bCompare = TRUE;
   
   }

   if(bCompare)
   {
    //바로 뒤에 있는 데이터를 읽어서
    SetFilePointer(m_hFile,sizeof(Info)*(i+1),NULL,FILE_BEGIN);
    ReadFile(m_hFile,&Info,sizeof(Info),&nRead,NULL);
    //데이터를 삭제한 곳에 덮어쓴다.
    SetFilePointer(m_hFile,sizeof(Info)*i,NULL,FILE_BEGIN);
    WriteFile(m_hFile,&Info,sizeof(Info),&nWrite,NULL);  

   }


  }
}

if(bCompare)
{
  CInfo blank;
  memset(&blank,0,sizeof(CInfo));
  SetFilePointer(m_hFile,sizeof(CInfo)*(nNumOfData -1),NULL,FILE_BEGIN);
  WriteFile(m_hFile,&blank,sizeof(blank),&nWrite,NULL);

  MessageBox(_T("삭제가 완료되었습니다."));

}

CloseHandle(m_hFile);

}

댓글 없음:

댓글 쓰기