C++ 将文件从数据输入到数组c++;
所以我正在做一个刽子手程序,我得到了一个输入文件,其中包含了我需要的单词。现在的问题是如何将我从文件中输入的单词放入数组中? 任何帮助都将不胜感激。谢谢C++ 将文件从数据输入到数组c++;,c++,arrays,file,sorting,input,C++,Arrays,File,Sorting,Input,所以我正在做一个刽子手程序,我得到了一个输入文件,其中包含了我需要的单词。现在的问题是如何将我从文件中输入的单词放入数组中? 任何帮助都将不胜感激。谢谢 enter code here #include <iostream> #include <fstream> #include <string> #include <ctime> #include <cstdlib> using namespace std; const int M
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;
常量int MAX_NUMS=200;//最大字数的常量。
常量int MAX_猜测=8;
常量字符串字母=“abcdefghijklmnopqrstuvxyz”;
//功能原型
char inputLetter();
int findChar(字符字母、字符串字);
字符串getGuessedWord(字符串加密、字符串字母加密);
//主要功能
int main()
{
//保存输入文件中的一个字
string secretWord;//保存要猜测的秘密字
字符串单词[MAX_NUMS];//保存输入文件中的单词列表
int randomValue;//保存秘密字的索引
int count=0;//保存文件中的字数
//声明名为myFile的ifstream对象并打开输入文件
弦线;
ifstream myfile(“p4words.txt”);
如果(myfile.is_open())
{
而(!myfile.eof())
{
getline(myfile,line);
cout如果您真的想将它们放入一个数组中,每次只需分配到下一个索引中:
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;
}
}
虽然对于这个确切的东西,我们有向量
,它不需要计数,也没有上限:
std::vector<std::string> words;
while (getline(myfile, line)) {
words.push_back(line);
}
std::向量词;
while(getline(myfile,line)){
字。推回(线);
}
除了巴里的答案
您还可以使用命令行执行并更改输入流,例如在创建.exe文件后(编译/生成后),如果文件名为“program1.exe”
文本文件为“input.txt”(在同一目录中),通过更改输入流,可以通过一个文本文件将输入指定给文件
在同一目录中打开命令行并写入
program1.exe<input.txt
program1.exearr[i];
}
std::cout您可以直接从istream_迭代器
(#include
)构造向量,例如:
std::vector<std::string> words((std::istream_iterator<std::string>(myfile)),
std::istream_iterator<std::string>());
将它们放在数组中,而不是将单词输出到控制台?并且std::vector
更好,因此您不必担心数组的“最大”大小。
program1.exe<input.txt>output.txt
std::vector<std::string> words((std::istream_iterator<std::string>(myfile)),
std::istream_iterator<std::string>());
std::vector<std::string> words{std::istream_iterator<std::string>(myfile), {}};