我遇到的问题是,我用 c 编写了一段代码来归档我的程序,当我用 c++ 编写相同的代码时,它不起作用。请帮我找出我在用 C++ 编写代码时犯的错误。
C 代码:
FILE* dict = fopen("small.txt", "r");
char word[MAX_LINE];
Node* root = newNode(); // pointer to main root of Trie
Node* temp;
while (fgets(word, MAX_LINE, dict) != NULL) {
temp = root;
buildTrie(temp, word);
}
fclose(dict);
C++ 代码:
ifstream infile;
char word[MAX_LINE];
Node* root = newNode(); // pointer to main root of Trie
Node* temp;
infile.open("small.txt");
while(infile)
{
for(int i =0;i<MAX_LINE;i++)
{
infile>>word[i];
temp = root;
buildTrie(temp, word);
}
}
infile.close();
最佳答案
如果我用 C++ 编写这样的代码,我可能会编写更像这样的代码:
std::string word;
while (std::getline(infile, word))
buildTrie(temp, word);
老实说,我怀疑我是否会编写完全一样的代码 - 我可能会将 trie
包装到一个类中,因此代码看起来更像是:
Trie t;
std::string word;
while std::getline(infile, word))
t.add(word);
关于c++ - 从 c 转换为 c++ 时出现归档错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188057/