【问题标题】:Read a complex file to pair <string,pair<map<string,string> string> > C++读取复杂文件以对 <string,pair<map<string,string> string> > C++
【发布时间】:2013-04-29 08:29:53
【问题描述】:

所以我们基本上想为我们的程序读取一个包含一些不同段的文本文件:

程序中的结构是一个缓存有:pair data> >

文件中的结构是(key既用作key又用作segment之间的分隔符)

key
headerKey : headerValue
headerKey : headerValue
......................
headerKey : headerValue
key
data
data
...
data
key

我们一直在尝试使用以下内容读取此内容,但它不读取日期格式 (RFC1123)。我们只将 headerValues 中的日期设为“08 Gmt”或类似的“XX gmt”。我们的阅读算法有什么问题,下面是 我们使用 : 作为分隔符,但它以不同的含义出现在日期格式中,即分割时间:

    try{

                // Create stream
                ifstream ifs(this->cacheFile.c_str(), ios::binary);

                // Read file to cache if stream is good
                if(ifs.good()){
                    while (! ifs.eof() ){
                        map<string,string> headerPairs;
                        string tmp;
                        string key;
                        string data;

                        getline(ifs, tmp);
                        while(tmp.empty()){
                            getline(ifs, tmp);
                            cout << "Empty line..." << "\n";
                            if(ifs.eof()){
                                cout << "End of File.."<< "\n";
                                break;
                            }
                        }

                        //After empty lines get "Key"
                        key = tmp;
                        getline(ifs, tmp);

                        //Get segment of header pairs
                        while(tmp != key){
                            StringTokenizer headerPair(tmp, ":", StringTokenizer::TOK_TRIM);
                            //StringTokenizer::Iterator it = headerPair.begin();
                            std::cout << *(headerPair.begin()) <<": " << *(headerPair.end()-1)<< std::endl;
                            string headerKey = *(headerPair.begin());
                            string headerValue = *(headerPair.end()-1);

                            headerPairs.insert(make_pair(headerKey, headerValue));
                            getline(ifs, tmp);
                        }

                        cout << "Added " << headerPairs.size() << " header pairs from cache" << "\n";
                        //tmp equals Key

                        while(tmp!=key){
                            getline(ifs, tmp);
                            cout << "Searching for header->data delimiter" << "\n";
                        }
                        cout << "Found header->data delimiter" << "\n";

                        //Get segment of data!
                        getline(ifs, tmp);
                        while(tmp != key){ 
                            data+=tmp;
                            getline(ifs, tmp);
                        }

                        cout << "DATA: " << data << "\n";
                        cout << "Ending delimiter:" << tmp << "\n";

                        this->add(key,make_pair(headerPairs, data));
                        cout << "Added: " << key << " to memory-cache" << endl;

                    }
                    ifs.close();
                }

            }
            catch (Exception &ex){
                cerr << ex.displayText() << endl;
            }

请提出更好的获取日期字符串的方法:

 DateTime now : Mon, 29 Apr 2013 08:15:57 GMT
 DateRetrieved from file: 57 GMT

简而言之:问题是我们使用 : 作为标头的分隔符,我想建议另一个安全的分隔符符号,即它不会在 HTTP 1.0 或 1.1 标头中找到。

【问题讨论】:

    标签: c++ parsing http ifstream


    【解决方案1】:

    您找不到故障安全分隔符,因为有人总是可能在数据中使用此参数。

    但是,要走的路是在插入数据之前避开数据中出现的任何分隔符。以下是CSV 的做法:

    "Date","Pupil","Grade"
    "25 May","Bloggs, Fred","C"
    "25 May","Doe, Jane","B"
    "15 July","Bloggs, Fred","A"
    "15 April","Muniz, Alvin ""Hank""","A"
    

    (当双引号在数据中并且需要转义时,请注意双“”)

    即使这种加倍字符的方法很常用,最流行的转义分隔符的方法是在字符前添加反斜杠“\”。

    如果您想了解更多相关信息,可以查看专用于此的Wikipedia page

    【讨论】:

    • 根据this,标题要包含一些标志。我们决定在文件中使用这个(✰)特殊字符来分隔标题✰值
    • @DavidKarlsson 但是如果用户在数据字段中使用这个字符呢?这仍然不是故障保险。
    • StringTokenizer 仅用于头部段,该段使用多字符键与其他段分隔...因此数据段不受该字符分割的影响 //BR
    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多