【发布时间】:2011-09-15 20:42:55
【问题描述】:
这两种读取输入文件的方法有什么区别?
1) 使用'ifstream.get()'
和
2) 将vector<char> 与ifstreambuf_iterator<char> 一起使用(我不太了解!)
(除了可以使用漂亮的向量方法的明显答案)
输入文件是 XML,如下所示,立即解析为 rapidxml 文档。 (在别处初始化,参见示例 main 函数。)
首先,让我向您展示两种编写“load_config”函数的方法,一种使用ifstream.get(),一种使用vector<char>
方法 1ifstream.get() 提供了工作代码和一个安全的 rapidXML 文档对象:
rapidxml::xml_document<> *load_config(rapidxml::xml_document<> *doc){
ifstream myfile("inputfile");
//read in config file
char ch;
char buffer[65536];
size_t chars_read = 0;
while(myfile.get(ch) && (chars_read < 65535)){
buffer[chars_read++] = ch;
}
buffer[chars_read++] = '\0';
cout<<"clearing old doc"<<endl;
doc->clear();
doc->parse<0>(buffer);
//debug returns as expected here
cout << "load_config: Name of my first node is: " << doc->first_node()->name() << "\n";
return doc;
}
方法 2 导致另一个库破坏了 rapidXML 文档 - 具体来说,调用 curl_global_init(CURL_GLOBAL_SSL) [见下面的主要代码] - 但我还没有将其归咎于 curl_global_init。
rapidxml::xml_document<> *load_config(rapidxml::xml_document<> *doc){
ifstream myfile("inputfile");
vector<char> buffer((istreambuf_iterator<char>(inputfile)),
istreambuf_iterator<char>( ));
buffer.push_back('\0');
cout<<"file looks like:"<<endl; //looks fine
cout<<&buffer[0]<<endl;
cout<<"clearing old doc"<<endl;
doc->clear();
doc->parse<0>(&buffer[0]);
//debug prints as expected
cout << "load_config: Name of my first node is: " << doc->first_node()->name() << "\n";
return doc;
}
主要代码:
int main(void){
rapidxml::xml_document *doc;
doc = new rapidxml::xml_document;
load_config(doc);
// this works fine:
cout << "Name of my first node is: " << doc->first_node()->name() << "\n";
curl_global_init(CURL_GLOBAL_SSL); //Docs say do this first.
// debug broken object instance:
// note a trashed 'doc' here if using vector<char> method
// - seems to be because of above line... name is NULL
// and other nodes are now NULL
// causing segfaults down stream.
cout << "Name of my first node is: " << doc->first_node()->name() << "\n";
我非常确定这一切都是在一个线程中执行的,但也许有一些超出我理解范围的事情发生。
我还担心我只是解决了一个症状,而不是一个原因......通过简单地更改我的文件加载功能。在这里向社区寻求帮助!
问题:为什么从向量转移到字符数组可以解决这个问题?
提示:我知道 rapidXML 使用了一些巧妙的内存管理,实际上直接访问输入字符串。
提示:上面的 main 函数创建了一个动态的(新的)xml_document。这不在原始代码中,而是调试更改的工件。原来的(失败的)代码声明了它并且没有动态分配它,但是出现了同样的问题。
另一个全面披露的提示(尽管我不明白它为什么重要) - 在这个由 rapidxml::xml_document 对象中的数据填充的混乱代码中还有另一个向量实例。
【问题讨论】:
-
其中的
sexy是什么?这是时装秀吗? -
由于唯一的区别是如何从文件中读取数据,这些问题似乎相关:stackoverflow.com/questions/116038/…stackoverflow.com/questions/195323/…
-
作为健全性检查,您能否将调试器设置为检查从 &buffer[0] 开始的内存,以便在 parse() 之前和之后的调用中查看它们是否在所有情况下都相同?
-
你仍然有这个错误。当你因为
chars_read < 65535为假而退出循环时,这意味着chars_read == 65535然后你访问了第65535 个位置,这是数组末尾的一个位置。 -
感谢 ybungalobill。根本不应该打折。注意到、赞赏和修复。
标签: c++ file-io libcurl rapidxml code-formatting