I am building a map from a text file using the std::unordered_map data structure, using the pair < uint64_t id, BaseDevice device >. BaseDevice is a structure, containing a 64-bit id and a string:
struct BaseDevice
{
public:
uint64_t id;
string ipAddress;
}
I read a file (assuming that it is correctly written) and build the map as follows:
char buffer[1024];
ifstream fin(myfile.c_str());
while(fin.eof() == false)
{
fin.getline(buffer, 1023);
StringTokenizer st(buffer, " ");
//Parse ID and address
unsigned int id = atoi(st.nextToken().c_str()); //get ID
string ipAddress = st.nextToken(); //get IP address
//Create the local structure and add to the map
BaseDevice dev;
dev.id = id;
dev.ipAddress = ipAddress;
myMap[id] = dev;
break;
}
The strange thing is that when I traverse my map, the ipAddress string seems to be (null), while both length() and size() are not.
unordered_map< string, BaseDevice >::const_iterator itr1;
for(itr1 = myMap.begin(); itr1 != myMap.end(); itr1++)
{
const BaseDevice& device = itr1->second;
fprintf(stdout, "id %lu ipAddress %s \n", myMap->first, device.ipAddress);
printf("Length is %d \n", device.ipAddress.length());
printf("Size is %d \n", device.ipAddress.size());
/*
OUTPUT:
id 2 ipAddress (null)
Length is 8
Size is 8
*/
}
I would like to ask you: how is this possible? Am I doing something wrong? Thanks.