[英]How to pass every line of a file to a function that takes const char[]?
所以基本上我有一个Trie
类,其中有一个ins()
方法。 此方法将const char []
类型作为参数。 另一方面,我有一个文件,其中包含逐行单词。
我想要做的是打开此文件并读取每一行,然后在每一行中调用ins()
方法,以便将所有单词插入到Trie
结构中。
这是我的代码:
Trie<bool, ALPHABET_SIZE>* trie = new Trie<bool,ALPHABET_SIZE>;
std::ifstream file("latin.txt"); // opening words file
if (!file){
std::cout << "Could not open the words file. Exiting the program." << "\n";
return 1;
}
char str[255];
while(file.getline(str, sizeof(str))){
trie->ins(str);
}
trie->printWords();
file.close();
delete trie;
return 0;
当我这样做时,我会遇到细分错误。 我确信Trie
中的所有方法都可以正常工作并且没有问题。
更新问题出在ALPHABET_SIZE
。 小测试将其分配给5,而我忘了将其分配给它的真实数字26。
从这里学到的教训是,有时您可能会认为自己知道问题的根源,但实际上,您还必须仔细检查其他事情。
永远不要对您的代码过于信任。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.