【发布时间】:2015-12-09 20:08:32
【问题描述】:
我遇到的问题是我用 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++ 代码,它不会正确地创建一个 trie 结构,而对于 C 代码来说它工作得非常好。
标签: c++ c file-handling