读取文件并存储到字符串数组中

时间:2022-11-27 15:43:43

I'm a student and I'm trying to make a hangman program. I'm given a file called words.dat that I'm supposed to use for my hangman program, but I don't know how to store the data and put it into a string array. I'm trying to create a function dedicated to importing the file. I don't know how may words will be in the dat file but my teacher said there would be 100 at most. This was my rough guess on how to do it, but I only get a lot of errors trying to compile it. How do I get the words from the dat file into the string array?

我是学生,我正在尝试制作一个刽子手计划。我给了一个名为words.dat的文件,我应该用于我的hangman程序,但我不知道如何存储数据并将其放入字符串数组中。我正在尝试创建一个专门用于导入文件的函数。我不知道在dat文件中会有多少单词,但我的老师说最多会有100个单词。这是我对如何做到这一点的粗略猜测,但我只是在尝试编译它时遇到了很多错误。如何将dat文件中的单词转换为字符串数组?

words.dat

COW
SCHOOL
KEY
COMPUTER

my function so far

我的功能到目前为止

#include <string>
#include <fsteam>
using namespace std;

bool initializeFile(string filename){
    int importWord = 0;
    string words[100];

    ifstream input;
    input.open(filename, 'r');
    if(input.fails){
        return false;
    }        
    /*
    Tring to make for loop that runs through 
    all the characters in input(dat file) and 
    store them into the words array. if the character isn't a letter it
    skips it and continues to the next row
    */

    for(int i=0;i< input.length();i++){
        if(input[i] =="\n"){
            importWord++;
        }
        if(input[i] != "\n"){
            words[importWord]=input[i];
        }
    }

    return true;
}

1 个解决方案

#1


0  

Since you have one word per line, you can use std::getline to read in the words.

由于每行有一个单词,您可以使用std :: getline来读取单词。

const int MAXIMUM_WORDS = 100;
std::string words[MAXIMUM_WORDS];
std::string word_from_file = 0;
int word_count = 0;
while (std::getline(word_file, word_from_file))
{
  if (word_count > MAXIMUM_WORDS)
  {
    break;
  }
  words[word_count] = word_from_file;
  ++word_count;
}

If you have covered the break statement, you can use multiply conditions:

如果您已经介绍了break语句,则可以使用乘法条件:

while ((word_count < MAXIMUM_WORDS) && (std::getline(word_file, word_from_file))

The above code is small enough that you can put the code into your main function, which eliminates the hassle of passing an array to an input function.

上面的代码足够小,您可以将代码放入main函数中,从而消除了将数组传递给输入函数的麻烦。

#1


0  

Since you have one word per line, you can use std::getline to read in the words.

由于每行有一个单词,您可以使用std :: getline来读取单词。

const int MAXIMUM_WORDS = 100;
std::string words[MAXIMUM_WORDS];
std::string word_from_file = 0;
int word_count = 0;
while (std::getline(word_file, word_from_file))
{
  if (word_count > MAXIMUM_WORDS)
  {
    break;
  }
  words[word_count] = word_from_file;
  ++word_count;
}

If you have covered the break statement, you can use multiply conditions:

如果您已经介绍了break语句,则可以使用乘法条件:

while ((word_count < MAXIMUM_WORDS) && (std::getline(word_file, word_from_file))

The above code is small enough that you can put the code into your main function, which eliminates the hassle of passing an array to an input function.

上面的代码足够小,您可以将代码放入main函数中,从而消除了将数组传递给输入函数的麻烦。