블로그 이미지
.
속눈썹맨

공지사항

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

bdb에 string 넣기

2004. 7. 9. 14:08 | Posted by 속눈썹맨
각 module별 key string null terminal 문자 취급
1. null을 포함하지 않는 경우(not include)
   dbt.size = strlen(str);
   dbt.size = str.size();
2. null을 포함하는 경우(include)
   dbt.size = strlen(str) + 1;
   dbt.size = str.size() + 1;

bdb의 value string인 경우는 두 경우 모두 쉽게 해결가능하여 별 문제가 되지 않으나 key인 경우는 문제가 됨. (bdb는 모든 입력을 binary로 취급하기 때문)

예제 프로그램)
#include <string>
#include <iostream>

using namespace std;

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string str1;
    string str2;
    string str3;
    string str4;
    string str5;
    string str6;
    string str7;
    string str8;

    string str9;
    string str10;
    string str11;
    string str12;
    string str13;
    string str14;
    string str15;
    string str16;

    str1.assign("abc\0\0\0", 1);
    cout << "str1.size() : " << str1.size() << endl;

    str2.assign("abc\0\0\0", 2);
    cout << "str2.size() : " << str2.size() << endl;

    str3.assign("abc\0\0\0", 3);
    cout << "str3.size() : " << str3.size() << endl;

    str4.assign("abc\0\0\0", 4);
    cout << "str4.size() : " << str4.size() << endl;

    str5.assign("abc\0\0\0", 5);
    cout << "str5.size() : " << str5.size() << endl;

    str6.assign("abc\0\0\0", 6);
    cout << "str6.size() : " << str6.size() << endl;

    str7.assign("abc\0\0\0", 7);
    cout << "str7.size() : " << str7.size() << endl;

    str8.assign("abc\0\0\0", 8);
    cout << "str8.size() : " << str8.size() << endl;


    str9.append("abc\0\0\0", 1);
    cout << "str1.size() : " << str9.size() << endl;

    str10.append("abc\0\0\0", 2);
    cout << "str2.size() : " << str10.size() << endl;

    str11.append("abc\0\0\0", 3);
    cout << "str3.size() : " << str11.size() << endl;

    str12.append("abc\0\0\0", 4);
    cout << "str4.size() : " << str12.size() << endl;

    str13.append("abc\0\0\0", 5);
    cout << "str5.size() : " << str13.size() << endl;

    str14.append("abc\0\0\0", 6);
    cout << "str6.size() : " << str14.size() << endl;

    str15.append("abc\0\0\0", 7);
    cout << "str7.size() : " << str15.size() << endl;

    str16.append("abc\0\0\0", 8);
    cout << "str8.size() : " << str16.size() << endl;

    return 0;
}
결과 :
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8

이 프로그램을 수행해 보면 append나 assign이 \0도 string에 저장하고 size로 취급한다는 것을 알 수 있다.


결론) C, C++이외의 다른 언어와의 호환성을 생각한다면
char*의 끝을 나타내는 \0은 넣지 않고 bdb에 저장하는 것이 좋다.