如何将来自输入TXT文件的数据读取到2 C 数组中
How to read data from input txt file into 2 C++ arrays?
txt文件
1 2 3 4 5 col A
2 3 4 5 6 col B
2 3 4 5 6 col C
2 3 4 5 6 col D
2 3 4 5 6 col E
我已经知道如何将数字存储到2D数组中。但是,问题还要求我将单词(包括空间)存储在一个维数阵列中。有人可以告诉我如何做吗?我是C 的新手。任何帮助将不胜感激。
这将需要一个基本的读取文件循环,该循环将逐行读取文件,然后按空格将其拆分。对于它将其推到2D数组的数字和单词到1D数组:
fstream file;
file.open("somefile.txt");
vector<vector<double>> nums;
vector<string> words;
string line;
while(getline(file, line))
{
vector<double> tempNums;
istringstream liney(line); int i = 0; string cell;
while(getline(liney, cell, ' '))
{
if(i < 5) tempNums.push_back(stoi(cell.c_str()));
else words.push_back(" " + cell);
i++;
}
nums.push_back(tempNums)
}
希望这会有所帮助。(这是基于上述格式)
相关文章:
- 如何在ros2中从rosbag2读取数据
- OpenCV 和从 OutputArrays 或 (Mat) 读取数据
- 使用C++从磁卡读卡器MT188读取数据
- HDF5 简单读取数据集失败
- 嵌套结构,从由空行分隔的文件中读取数据
- 不要从输入队列套接字读取数据
- 从文件中读取数据,操作员>>
- 如何从excel文件中读取数据并存储到变量中?
- C++:从文件中读取x y数据会产生无限循环吗
- 无法通过FT232RL读取数据
- 如何从QNetworkReply读取数据?
- C++ 逐行从文件(包含空格)读取数据
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 我们可以从 TXT 文件中读取数据并使用 C 和 C++ 将其保存到 SQL 数据库吗?
- 从多个 tcp 连接读取数据
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- Qt TCP服务器不从客户端读取数据
- 累积从 C++11 中的文件中读取数据的整数
- 从 txt 文件中读取数据的最简单方法
- 从 txt 文件中读取数据的 Getline