读入文件到一个数组 [英] Reading a file into an array

查看:117
本文介绍了读入文件到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读一个文本文件,输入其内容到一个数组。然后我想显示在命令行中数组的内容。

I would like to read a text file and input its contents into an array. Then I would like to show the contents of the array in the command line.

我的想法是用打开文件:

My idea is to open the file using:

inFile.open("pigData.txt")

和再利用获取文件的内容:

And then to get the contents of the file using:

inFile >> myarray [size]

和随后使用用于循环显示的内容。

And then show the contents using a for loop.

我的问题是,该文件,我想读的话包含,我不知道如何获得一个完整的单词作为数组中的元素。此外,让我们说的话都用空格分开,这样的:

My problem is that the file I am trying to read contain words and I don't know how to get a whole word as an element in the array. Also, let's say that the words are divided by spaces, thus:

您好再见

可以在该文件中找到。我想整行你好再见读成平行数组的元素。我该怎么做?

Could be found on the file. I would like to read the whole line "hello goodbye" into an element of a parallel array. How can I do that?

推荐答案

有关的背景下,你可以提供一个链接到您previous问题,有关存储字的两个列表以不同的语言。在那里,我提供的读取文本文件的内容到一个数组的例子:

For context, you could have provided a link to your previous question, about storing two lists of words in different languages. There I provided an example of reading the contents of a text file into an array:

const int MaxWords = 100;
std::string piglatin[MaxWords];
int numWords = 0;
std::ifstream input("piglatin.txt");
std::string line;
while (std::getline(input, line) && numWords < MaxWords) {
  piglatin[numWords] = line;
  ++numWords;
}
if (numWords == MaxWords) {
  std::cerr << "Too many words" << std::endl;
}

您不能有 有一个平行排列。东西向平行,必须有至少两个。对于词的平行阵列,你可以使用一个声明如下:

You can't have one parallel array. For something to be parallel, there must be at least two. For parallel arrays of words, you could use a declarations like this:

std::string piglatin[MaxWords];
std::string english[MaxWords];

然后,你必须从文件填充阵列两种选择:

Then you have two options for filling the arrays from the file:


  1. 读取整行,和分割线成两个字基于在第一空间是:

  1. Read an entire line, and the split the line into two words based on where the first space is:

while (std::getline(input, line) && numWords < MaxWords) {
  std::string::size_type space = line.find(' ');
  if (space == std::string::npos)
    std::cerr << "Only one word" << std::endl;
  piglatin[numWords] = line.substr(0, space);
  english[numWords] = line.substr(space + 1);
  ++numWords;
}


  • 读取一次一个字,而假设的每一行有它究竟是两个词。在&GT;&GT; 运营商将在同一时间自动读一个字。 (如果每行没有完全相同的两个单词,那么你就会有问题。试试吧,看看事情如何出错的。真的,获得经验的一个错误,当你的知道的病因是什么将帮助你在未来,当你的的知道原因是什么。)

  • Read one word at a time, and assume that each line has exactly two words on it. The >> operator will read a word at a time automatically. (If each line doesn't have exactly two words, then you'll have problems. Try it out to see how things go wrong. Really. Getting experience with a bug when you know what the cause is will help you in the future when you don't know what the cause is.)

    while (input && numWords < MaxWords) {
      input >> piglatin[numWords];
      input >> english[numWords];
      ++numWords;
    }
    


  • 现在,如果你真的有一个有一个的阵列的两个元素的,那么您需要定义另一种数据结构,因为数组只能有一个东西中的每个元素。定义的东西,可以同时容纳两个字符串:

    Now, if you really one one array with two elements, then you need to define another data structure because an array can only have one "thing" in each element. Define something that can hold two strings at once:

    struct word_pair {
      std::string piglatin;
      std::string english;
    };
    

    然后你就只有一个数组:

    Then you'll have just one array:

    word_pair words[MaxWords];
    

    您可以填写像这样:

    while (std::getline(input, line) && numWords < MaxWords) {
      std::string::size_type space = line.find(' ');
      if (space == std::string::npos)
        std::cerr << "Only one word" << std::endl;
      words[numWords].piglatin = line.substr(0, space);
      words[numWords].english = line.substr(space + 1);
      ++numWords;
    }
    

    注意如何code索引到数组来寻找下一个 word_pair <​​/ code>对象,然后它使用了运营商才能到 piglatin 英语字段是必要的。

    Notice how the code indexes into the words array to find the next word_pair object, and then it uses the . operator to get to the piglatin or english field as necessary.

    这篇关于读入文件到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆