从VS2013中VC++中具有多个部分的文件读取数据
read data from a file with multiple sections from VC++ in VS2013
我需要从VS2013中的VC++读取txt文件。
在文件中,有多个部分:
#section1
head1,head2,head3
dcscsa, sdew, safce
.....
#section2
head1,head2,head3, head4,head5
112,633,788,632,235
.....
我需要将行保存到不同的数据结构中 对于第 1 节: 地图第1节>
for section12
mapSection2<string, map<string, int>>
我可以使用代码吗:
string aLine;
getline(file, aLine);
stringstream ss(aLine);
int cnt = 0;
if (file.good())
{
while (!file.eof())
{
string substr;
getline(file, aLine);
stringstream ss(aLine);
while (ss.good())
{
// how to save data to different map for different section?
}
此外,我可以将整个文件加载到数据集中,然后在逐行读取文件时处理每一行或处理每一行。
哪一个更有效?
谢谢
我可以使用代码吗:...
差一点。
用:
int sectionNo = 0;
while (getline(file, aLine))
{
if (aLine.empty()) continue;
if (aLine[0] == '#')
{
++sectionNo;
getline(file, aLine); // read headings... use them if you like
continue;
}
std::stringstream ss(aLine);
switch (sectionNo)
{
case 1:
if (ss >> a >> b >> c >> std::skipws && ss.eof())
... use a, b, c ...
else
throw std::runtime_error("invalid data in section 1 " + aLine);
break;
case 2:
...
}
此外,我可以将整个文件加载到数据集中,然后在逐行读取文件时处理每一行或处理每一行。
哪一个更有效?
几乎总是后者。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量