【问题标题】:c++ How to read from a file into array one word at a timec ++如何一次一个单词从文件中读取到数组中
【发布时间】:2018-11-18 11:06:17
【问题描述】:

知道这是一个愚蠢的问题!

但我只是不明白如何使用 c++ 一次将我的文件读入数组中

这是我试图做的代码 - 一些尝试的输出。

void readFile()
{
   int const maxNumWords = 256;
   int const maxNumLetters = 32 + 1;
   int countWords = 0;
   ifstream fin;
   fin.open ("madLib.txt");
   if (!fin.is_open()) return;

   string word;
   while (fin >> word)
   {
      countWords++;
      assert (countWords <= maxNumWords);
   }

   char listOfWords[countWords][maxNumLetters];
   for (int i = 0; i <= countWords; i++)
   {
      while (fin >> listOfWords[i]) //<<< THIS is what I think I need to change 
                                    //buggered If I can figure out from the book what to
      {
         // THIS is where I want to perform some manipulations - 
         // BUT running the code never enters here (and I thought it would)
         cout <<  listOfWords[i];
      }
   }
}

我正在尝试将 madLib.txt 文件中的每个单词(由单词之间的空格定义)放入 listOfWords 数组中,以便我可以逐个字符地执行一些字符串操作。

很明显,我可以从文件中读取并将其放入字符串变量中——但这不是作业(是的,这是针对大学的编码课程)

我已经从一个文件中读取了整数到一个数组中 - 但我不太明白如何在这里应用它......

【问题讨论】:

  • 第一个循环读取到文件末尾,第二个循环尝试读取更多内容。在文件末尾不能成功。
  • 我刚刚用这个 i
  • @kiltannen 您还必须clear() 输入流。忘记了对不起。但我建议您使用我在回答中勾勒出的方法。

标签: c++ arrays file


【解决方案1】:

我能想到的最简单的解决方案是:

void readFile()
{
   ifstream fin;
   fin.open ("madLib.txt");
   if (!fin.is_open()) return;

   vector<string> listOfWords;
   std::copy(std::istream_iterator<string>(fin), std::istream_iterator<string>()
            , std::back_inserter(listOfWords));
}

无论如何,您在问题中表示您希望一次阅读一个单词并应用操作。因此,您可以执行以下操作:

void readFile()
{
   ifstream fin;
   fin.open ("madLib.txt");
   if (!fin.is_open()) return;

   vector<string> listOfWords;
   string word;
   while(fin >> word) {
        // THIS is where I want to perform some manipulations
        // ...
        listOfWords.push_back(word);
   }
}

【讨论】:

  • 虽然我喜欢这个答案的明显优雅,但我们还没有涵盖向量。我相信这将是一个问题,因为课程很清楚我不能复制代码。我很想了解您对 fin.seekg(0) 的其他建议的情况; - 而且我认为我可以对此进行研究并感到放心,它不会违反课程限制...
  • @kiltannen 好吧,你不能使用std::vector 是一个愚蠢的限制,只是你个人的问题。本着 Stack Overflow 的问答应该对每个人都有帮助的精神,我将在此处留下我的答案。
  • 留下您写的优雅答案完全没有问题。只是想弄清楚输入流在 clear() 上的语法和位置......
  • @kiltannen 就在您再次开始从流中读取之前。在你的第一个循环之后,仍然设置了 eof 标志,所以你必须清除它。
  • 所以我想如果我关闭文件并再次打开它就会完成这个。我刚刚阅读了一些 .seekg(0) 的文档,遗憾的是它对我来说并不像对你那么简单。我想我最终会到达那里。感谢您的提示!
【解决方案2】:

关于πάντα ῥεῖ的建议

我试过这个:

void readFile()
{
   int const maxNumWords = 256;
   int const maxNumLetters = 32 + 1;
   int countWords = 0;
   ifstream fin;
   fin.open ("madLib.txt");
   if (!fin.is_open()) return;

   string word;
   while (fin >> word)
   {
          countWords++;
          assert (countWords <= maxNumWords);
   }
   fin.clear();
   fin.seekg(0);
   char listOfWords[countWords][maxNumLetters];
   for (int i = 0; i <= countWords; i++)
   {
  while (fin >> listOfWords[i]) //<<< THIS did NOT need changing
  {
     // THIS is where I want to perform some manipulations - 
     cout <<  listOfWords[i];
   }
}

它对我有用。我确实认为使用向量更优雅,因此接受了这个答案。

还建议将此作为自我回答而不是作为编辑发布 - 我有点同意这是明智的,所以我继续这样做了。

【讨论】:

    【解决方案3】:

    最简单的方法是使用 STL 算法...这是一个示例:

    #include <iostream>
    #include <iomanip>
    #include <iterator>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        vector<string> words;
    
        auto beginStream = istream_iterator<string>{cin};
        auto eos = istream_iterator<string>{};
        copy(beginStream, eos, back_inserter(words));    
    
        // print the content of words to standard output
        copy(begin(words), end(words), ostream_iterator<string>{cout, "\n"});
    }
    

    当然,除了cin,您可以使用任何istream 对象(如file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多