将每个单词从文件读入C ++中的char数组

时间:2015-05-09 02:48:24

标签: c++ arrays

使用 public DownloadWebpageTask(AsyncResult callback) { this.callback = callback; } 读取文件然后将每个单词存储在char数组中的正确方法是什么?这个char数组最终将用于将当前单词输入到哈希表中。

我的代码:

ifstream()

1 个答案:

答案 0 :(得分:2)

除非你绝对必须,否则不要使用char数组。将它们读入字符串,然后将字符串放入哈希表中。如果您没有哈希表,我可以推荐std::set吗?可能与您的需求一致。

读东西。试一试:

int main()
{
    ifstream fin("Dict.txt");
    set<string> dict;  // using set as a hashtable place holder.

    if (fin.is_open())
    {
        cout << "File Opened successfully" << endl;
        string word;
        while (getline(fin, word, '\0'))
        {  /* getline normally gets lines until the file can't be read, 
                but you can swap looking for EOL with pretty much anything 
                else. Null in this case because OP was looking for null */
           //insert into hash table
            dict.insert(word); //putting word into set
        }
        cout << endl;
    }
    else
    {
        cout << "File could not be opened." << endl;
    }
    system("pause"); // recommend something like cin >> some_junk_variable
    return 0;
}