【问题标题】:Entering file from data to an array c++将文件从数据输入到数组c ++
【发布时间】:2015-01-25 00:54:38
【问题描述】:

所以我正在开发一个hangman 程序,并且我得到了包含我需要的单词的输入文件。现在的问题是如何将我从文件中输入的单词放入数组中? 任何帮助将不胜感激。谢谢!

enter code here
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

const int MAX_NUMS = 200;   // Constant for the maximum number of words.
const int MAX_GUESSES = 8;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";


//function prototypes
char   inputLetter();
int    findChar(char letter, string word);
string getGuessedWord(string secretWord, string lettersGuessed);

//main function
int main()
{
                // holds one word from input file
string secretWord;              // holds secret word to be guessed
string words[MAX_NUMS];         // holds list of words from input file
int randomValue;                // holds index of secret word
int count = 0;                  // holds number of words in the file

// Declare an ifstream object named myFile and open an input file
string line;

ifstream myfile ("p4words.txt");

if (myfile.is_open())

{

    while (! myfile.eof() )

    {

        getline (myfile,line);

        cout << line << endl;

    }

    myfile.close();

}

else cout << "Unable to open file";

// Input words from a file into words array

cout << count << " words loaded." << endl;

srand(static_cast<unsigned int>(time(0)));

【问题讨论】:

  • 将它们放在数组中,而不是或者除了将单词输出到控制台之外? std::vector 更好,因此您不必担心数组的“最大”大小。

标签: c++ arrays file sorting input


【解决方案1】:

如果你真的想将它们放入一个数组中,只需每次都分配到下一个索引中:

while (getline(myfile, line)) {
    words[count] = line;
    ++count;
    if (count == MAX_NUMS) {
        // this is all the space we have
        break;
    }
}

或者只是getline直接进入数组:

while (getline(myfile, words[count])) {
    ++count;
    if (count == MAX_NUMS) {
        break;
    }
}

虽然对于这个确切的东西我们有vector,它不需要计数并且没有上限:

std::vector<std::string> words;

while (getline(myfile, line)) {
    words.push_back(line);
}

【讨论】:

    【解决方案2】:

    除了巴里的回答

    如果文件名为“program1.exe”,您还可以使用命令行执行并更改输入流,例如在创建 .exe 文件后(编译/构建后) 并且文本文件是“input.txt”(在同一目录中)输入可以通过这个文本文件通过更改输入流提供给文件

    在同一目录下打开命令行并写入

     program1.exe<input.txt 
    

    原来的程序根据你的需要可以像

    int main(){
        int arr[6];
        for(int i=1;i<=5;i++){
            std::cin>>arr[i];
        }
        std::cout<<"data entered";
    }
    

    而 input.txt 就像

    52 14 24 23 45

    同样,您可以通过在命令行中写入来更改输出流并将输出保存到不同的文件中,例如 output.txt

    program1.exe<input.txt>output.txt     
    

    【讨论】:

      【解决方案3】:

      您可以直接从istream_iterator (#include &lt;iterator&gt;) 构造向量,例如:

      std::vector<std::string> words((std::istream_iterator<std::string>(myfile)), 
              std::istream_iterator<std::string>());
      

      (std::istream_iterator&lt;std::string&gt;(myfile)) 中的双 () 需要避免最麻烦的解析。在 C++11 中可以简单地使用大括号初始化并编写

      std::vector<std::string> words{std::istream_iterator<std::string>(myfile), {}}; 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-08
        • 1970-01-01
        • 2019-02-27
        • 2020-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多