C++ 将ifstream文件和每一行读入一个数组

C++ 将ifstream文件和每一行读入一个数组,c++,C++,我希望任何人都能帮助我,或者至少理解我正在努力实现的目标。 我试着在网上搜索这个问题,但我没有找到答案,或者至少不了解其他人做了什么 因此,我试图将file.txt中的每一行读取到一个数组中。 例如,my file.txt包含: 3 labas ka tu nu tai davai (只是一个例子) 我想把每一行都写进我的 string sentenses[CMax]; (CMax设置为10000) 经过数小时的网络搜索和自我修补,我最终发现: ifstream KC;

我希望任何人都能帮助我,或者至少理解我正在努力实现的目标。 我试着在网上搜索这个问题,但我没有找到答案,或者至少不了解其他人做了什么

因此,我试图将file.txt中的每一行读取到一个数组中。 例如,my file.txt包含:

3
labas 
ka tu
nu tai davai
(只是一个例子)

我想把每一行都写进我的

string sentenses[CMax]; 
(CMax设置为10000)

经过数小时的网络搜索和自我修补,我最终发现:

    ifstream KC;
    KC.open(langPicked.c_str());
      KC >> m;
      for (int o = 0; o < m; o++) {
        KC.getline(sentenses[o], 255); 
        if(KC) cout << sentenses[o] << endl;
      }
    KC.close();
ifstreamkc;
KC.open(langpick.c_str());
KC>>m;
对于(int o=0;o
  cout << sentenses[2] << endl;

cout谢谢Rustyx

解决方案是更改getline

对于任何感兴趣的人。此代码正在工作:

ifstream KC;
KC.open(langPicked.c_str());
  KC >> m;
  for (int o = 0; o < m; o++) {
    getline(KC, sentenses[o]);
  }
KC.close();
ifstreamkc;
KC.open(langpick.c_str());
KC>>m;
对于(int o=0;o

p.s.idk如何将rustyx的答案附加到“已解决”部分,因此我发表了自己的评论。对不起,请查看此伪代码:

string filename; //load the filename
cout << "Give the name of the file:\n";
cin >> filename;
cout << endl; 

ifstream file;

string txt = ".txt";

filename.append(txt);  //append .txt to the filename

file.open(filename);   //open this file

if(file.is_open()) {
    const int CMax = 10000;
    string senteces[CMax];
    int o = 0;

    while (getline(file, senteces[o])) 
        ++o;

    cout << senteces[2] << endl; //it will print "ka tu"
    cout << senteces[0] << endl; //it will print "3"
}
string filename;//加载文件名
cout>文件名;

什么是“海量”?“海量”可能是误译…有没有类似的建议词?对不起,我所指的海量是(使用谷歌翻译):array@tima我收到一个错误:D:\Lith\Codeblocks\uzd\skaiciiuokle 2.0\main.cpp | 88 |错误:调用'std::basic_ifstream::getline(std::u cx11::string&,int)时没有匹配的函数Rustyx的建议要求你使用:<代码> GETLIN(KC,句子[O]); PLS读取C++没有可变长度数组。谢谢@ RuStux的建议!我已经编辑了上面的代码。