我有以下文本文件:
0x2c200000 -3 1
0x2c200002 1 0
0x2c200004 -3 1
0x2c200006 2 3
0x2c200008 -1 2
0x2c20000a -2 1
0x2c20000c 3 1
我尝试从此文本文件中创建一个std :: map,其中第一列是键,第二和第三列是对值。我正在使用以下简单代码来做到这一点:
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
typedef std::pair<int,int> ffp;
FILE *map;
std::map<int, ffp > m_idmap;
std::map<int, int> m_onOffmap;
void IDTest_3() {
// Init the Id maps
map = fopen("test1.txt","r");
if(map==NULL){
std::cout << "Do not have idmap text file !" << std::endl;
return;
}
int id,BarEndCap,Sampling;
for(unsigned i=0; i<23; ++i) {
if ( fscanf(map, "%d %d %d", &id, &BarEndCap, &Sampling) !=3 )
{
std::cout << "Corrupted file ? "<<std::endl;
return;
}
// m_onOffmap[BarEndCap] = Sampling;
m_idmap[id] = std::make_pair(BarEndCap,Sampling);
}
fclose(map);
std::cout << " Test Bar endcap " << BarEndCap << std::entl;
}
当我编译这段代码时,我收到消息“文件损坏?”看起来我的fscanf无法正常运行。你知道我在做什么错吗?
答案 0 :(得分:4)
您正在尝试用%d
说明符匹配十六进制数(第一列),该说明符期望以10为底的整数。
尝试使用%x
(仅十六进制)或%i
(自动检测基数)!
有关更多信息,请参见std::fscanf's
format specifiers。
答案 1 :(得分:-6)
entl
您有endl
而不是else
。有了这一更改,它就会为我编译。