STL을 이용해서 파일로그 처리를 하려고 합니다.

luscent의 이미지

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을 처음 씁니다. ) 도움 부탁드립니다.

progcom의 이미지

STL에 포인터를 담으면, '못찾습니다'.

포인터는 주소값이기 때문에, 새로 malloc한 주소와 이전에 넣은 주소가 다를수 밖에 없습니다.

std::string을 사용하시거나, 혹은 별도의 비교 함수를 제공해주셔야합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.