简体   繁体   English

如何从文件一次读入数组一个单词

[英]c++ How to read from a file into array one word at a time

I know this is a dumb question! 知道这是一个愚蠢的问题!

But I just CAN NOT get my head around how to read my file into an array one word at a time using c++ 但是我无法理解如何使用c ++一次将一个文件读取到一个数组中

Here is the code for what I was trying to do - with some attempted output. 这是我正在尝试执行的代码-带有一些尝试的输出。

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];
      }
   }
}

I am trying to get each word (defined by a space between words) from the madLib.txt file into the listOfWords array so that I can then perform some character by character string manipulation. 我正在尝试将madLib.txt文件中的每个单词(由单词之间的空格定义)放入listOfWords数组中,以便随后可以通过字符串操作执行某些字符。

Clearly I can read from a file and get that into a string variable - BUT that's not the assignment (Yes this is for a coding class at college) 显然,我可以从文件中读取并将其放入字符串变量中-但不是作业(是的,这是针对大学的编码班)

I have read from a file to get integers into an array - but I can't quite see how to apply that here... 我已经读过一个文件,将整数放入数组中-但我不太明白如何在此处应用它。

The simplest solution I can imagine to do this is: 我能想到的最简单的解决方案是:

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));
}

Anyways, you stated in your question you want to read one word at a time and apply manipulations. 无论如何,您在问题中说过您想一次读一个字并进行操作。 Thus you can do the following: 因此,您可以执行以下操作:

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);
   }
}

On the suggestion of πάντα ῥεῖ 关于πάνταῥεῖ的建议

I've tried this: 我已经试过了:

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];
   }
}

and it has worked for me. 它为我工作。 I do think using vectors is more elegant, and so have accepted that answer. 我确实认为使用向量更优雅,因此已经接受了该答案。

The suggestion was also made to post this as a self answer rather than as an edit - which I kind of agree is sensible so I've gone ahead and done so. 还提出了将其发布为自我回答而不是编辑的建议-我有点同意这是明智的,所以我继续这样做。

The most simple way to do that is using the STL algorithm... Here is an example: 最简单的方法是使用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"});
}

Instead of cin of course, you can use any istream object (like file ) 当然可以代替cin来使用任何istream对象(例如file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM