STL을 이용해서 파일로그 처리를 하려고 합니다.
글쓴이: luscent / 작성시간: 금, 2004/04/09 - 1:42오후
typedef struct { char *intnr; char *intdate; char *inttime; unsigned int internr; unsigned int status; unsigned int contab; unsigned int tottime; unsigned int itutime; unsigned int area; unsigned int teltime; unsigned int scrcnt; } IV; typedef map<char * , vector<IV *> > htIv; bool splitToVector(char *line, IV* iv, TTreeView *tv) { //Vector; vector<IV *> v; char msgstring[100]; if(iv == NULL) { MessageDlg("메모리 초기화에 실패하였습니다. \n\n프로그램을 종료합니다!", mtInformation , TMsgDlgButtons() << mbOK, 0); exit(1); return false; } AnsiString *ans = new AnsiString(line); iv->intnr = (char *)malloc(8+1); strcpy(iv->intnr,ans->SubString(1,8).c_str()); iv->intdate = (char *)malloc(8+1); strcpy(iv->intdate,ans->SubString(9, 8).c_str()) ; iv->inttime = (char *)malloc(4+1); strcpy(iv->inttime,ans->SubString(17,4).c_str()) ; iv->internr = atoi(ans->SubString(21,8).c_str()); iv->status = atoi(ans->SubString(29,2).c_str()); iv->contab = atoi(ans->SubString(31,2).c_str()); iv->tottime = atoi(ans->SubString(33,6).c_str()); iv->itutime = atoi(ans->SubString(39,6).c_str()); iv->area = atoi(ans->SubString(45,6).c_str()); iv->teltime = atoi(ans->SubString(51,6).c_str()); iv->scrcnt = atoi(ans->SubString(57,4).c_str()); //retrieve; if(ht_of_InterViewer[iv->intnr].empty()) { v.push_back(iv); ht_of_InterViewer[iv->intnr] = v; ShowMessage(ht_of_InterViewer[iv->intnr][0]->intnr ); } else { ht_of_InterViewer[iv->intnr].push_back(iv); ShowMessage(ht_of_InterViewer[iv->intnr][ht_of_InterViewer[iv->intnr].size()]->intnr ); } tv->Items->Add(NULL, iv->intnr); sprintf(msgstring, "%s 의 아이템 개수는 %d 개 입니다", iv->intnr, ht_of_InterViewer[iv->intnr].size()); ShowMessage(msgstring); tv->Repaint() ; tv->Refresh() ; return true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int fd, bytes=0; char *buf = (char *)malloc(MAXLINELENGTH+1); TTreeNode *node; vector<IV *>v; IV *iv; if(OpenDialog1->Execute()) if((fd = open(OpenDialog1->FileName.c_str(), O_RDONLY)) == -1) { ShowMessage("파일을 열수 없습니다"); return ; } TreeView1->Items->Clear(); TreeView1->Items->Add(NULL, OpenDialog1->GetNamePath() ); node = TreeView1->Items->Item[0]; while(read(fd, buf, MAXLINELENGTH+1)) { iv = (IV *)malloc(sizeof(IV)); bytes += MAXLINELENGTH; splitToVector(buf, iv, this->TreeView1); } ShowMessage("파일 로딩이 끝났습니다"); free(buf); close(fd); }
소스를 첨부하였습니다.
위의 소스는 12만개의 라인이 저장되어 있는 텍스트 파일을
IV구조체형식으로 저장하고
제일 앞의 8자리를 키로 하여
map<char *, vector<IV *> >으로 저장하려고 하는 소스입니다.
메모리 부분이나. map으로 저장하는 부분에 어떤 점이 잘못되어져 있는지
(사실 stl을 처음 씁니다. ) 도움 부탁드립니다.
Forums:
STL에 포인터를 담으면, '못찾습니다'.포인터는 주소값이기 때
STL에 포인터를 담으면, '못찾습니다'.
포인터는 주소값이기 때문에, 새로 malloc한 주소와 이전에 넣은 주소가 다를수 밖에 없습니다.
std::string을 사용하시거나, 혹은 별도의 비교 함수를 제공해주셔야합니다.
댓글 달기