Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将外部文件中的值放入数组?_C++ - Fatal编程技术网

C++ 如何将外部文件中的值放入数组?

C++ 如何将外部文件中的值放入数组?,c++,C++,我试图读取一个外部文件,并将文件中的所有字符串放入字符串类型的数组中 这是我的主要职能: #include <iostream> #include "ReadWords.h" #include "Writer.h" #include <cctype> #include <string> using namespace std; int main() { int count; const int size = 10; string

我试图读取一个外部文件,并将文件中的所有字符串放入字符串类型的数组中

这是我的主要职能:

#include <iostream>
#include "ReadWords.h"
#include "Writer.h"
#include <cctype>
#include <string>

using namespace std;

int main() {

    int count;
    const int size = 10;
    string word_search[size];
    string word;

    cout << "Please enter a filename: " << flush;
    char filename[30];
    cin >> filename;

    ReadWords reader(filename);
    while (reader.isNextWord()){
        count = count + 1;
        reader.getNextWord();

    }

    cout << "Please enter the name of the file with the search words: " << flush;

    char filename1[30];
    cin >> filename1;

    ReadWords reader1(filename1);
    while (reader1.isNextWord()) {
#包括
#包括“ReadWords.h”
#包括“Writer.h”
#包括
#包括
使用名称空间std;
int main(){
整数计数;
常数int size=10;
字符串字搜索[大小];
字符串字;
cout文件名;
ReadWords阅读器(文件名);
while(reader.isNextWord()){
计数=计数+1;
reader.getNextWord();
}
cout文件名1;
ReadWords reader1(文件名1);
while(reader1.isNextWord()){
这就是我试图将字符串存储在一个名为word_search的数组中的地方,但目前该操作不起作用。如何将字符串存储在数组中

        for(int i = 0; i < size; i++){
            word_search[i] = word;

        }
    }
for(int i=0;i
这就是我打印数组内容的地方,以查看我是否成功

    cout << word_search << endl;



    return 0;
}
cout你的意思可能是

    size_t count = 0;
    while (reader.isNextWord()){
        word_search[count] = reader.getNextWord();
        ++count;
    }

也可以考虑使用STD::vector代替数组,也可以使用变量“Word”。 要打印内容,请使用

   for (size_t i = 0; i < size; ++i)
        cout << word_search[i] << endl;
(大小i=0;i
有什么问题吗?如果设置正确,for循环可以很好地工作。我的第一次猜测告诉我你想用
word\u search[i]=word;
替换
word\u search。推回(word);
@CaptainGiraffe:啊,是的,那个著名的函数
((std::string)[10])::推回
@lightness race循环你比我读得更透彻了。我推荐你这样做。我也有点印象深刻。@giraffe船长:发现代码中没有向量并不太麻烦。
   for (size_t i = 0; i < size; ++i)
        cout << word_search[i] << endl;