我正在寻找最好的/最简单的方法从txt文件中获取数据并将这些数据合并到C++的映射容器中。我有一个二维txt文件与所有无符号整数整数。如果这会更容易,我也可以将文件重新格式化为CSV。TXT或CSV到C++映射
这里是我尝试导入数据然后打印出来的代码。
代码片段:
static const int rowamount = 13;
// Store pairs (Time, LeapSeconds)
map<int, int> result;
// Read data from file
ifstream input("Test.txt");
for (int currrow = 1; currrow <= rowamount; currrow++)
{
int timekey;
input >> timekey;
int LeapSecondField;
input >> LeapSecondField;
// Store in the map
result[timekey] = LeapSecondField;
}
for (auto it = result.begin(); it != result.end(); ++it)
{
cout << it->first << endl;
cout << it->second << endl;
}
文件:
173059200 23
252028800 24
315187200 25
346723200 26
393984000 27
425520000 28
457056000 29
504489600 30
551750400 31
599184000 32
820108800 33
914803200 34
1025136000 35
我的输出是这样的:
1606663856
32767
我不知道为什么会这样。
您确定您使用的是正确的输入文件吗? – P0W
是的,我是。在这个程序中,我在桌面上创建一个名为LeapList.txt的文件,然后访问同一个文件。 – raoul
你可以在for循环之前检查'std :: cout << input.good()<< std :: endl;'吗? – P0W