将 ifstream 文件和每一行读取到数组中
read ifstream file and each line into a array
我希望有人能够帮助我,或者至少理解我想要实现的目标。 我试着在网上搜索这个,但我没有找到答案,或者至少不明白别人做了什么。
因此,我试图将文件.txt的每一行读取到数组中。 例如,我的文件.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();
但是,令人惊讶的是,它不起作用。
基本上,我想要实现的是,来自 KC 的每一行都将添加到>> sentenses[o] 数组中,这样我就可以像我键入以下内容来计算出我想要的任何行
:cout << sentenses[2] << endl;
它将输出 2 行:
卡图
弹出的错误:
D:\Lith\Codeblocks\uzd\Skaiciuokle 2.0\main.cpp|88|error: 没有匹配函数来调用 'std::basic_ifstream::getline(std::__cxx11::string&, int('|
注意: 我发布的代码可能,并且在某种程度上是错误的形状或形式。我刚刚开始尝试自己学习 c++。如果您了解我想要做什么,请随时用适合我情况的自己编写的代码来回答。提前谢谢你
看看这个伪代码:
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"
}
谢谢Rustyx
解决方案是更改Getline。
对于任何1感兴趣。此代码正在工作:
ifstream KC;
KC.open(langPicked.c_str());
KC >> m;
for (int o = 0; o < m; o++) {
getline(KC, sentenses[o]);
}
KC.close();
附言idk如何将rustyx的答案附加到"已解决"部分,所以我发表了自己的评论。不好意思
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 如何从一行中读取特定数量的字符?(C++)
- 读取最后一行代码算法 - c++ 时出现问题
- 而(!inputfile.eof())只读取第一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 需要从存档中读取一行文本.txt直到找到"hhh",然后转到下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文件中的一行读取特定数据-C++
- 使用std::getline()读取一行
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 从文件读取并在一行中写入 cout
- 将 ifstream 文件和每一行读取到数组中
- 从文件 c++ 中的下一行读取
- 在同一行读取具有多个类型的文件
- 写入终端并从同一行读取
- 如何在c++中从最后一行读取文本文件
- 逐行读取数组,每一行读取3个值
- 在同一行读取字符串和整数
- 从一行读取hh:mm格式的时间