Store contents of text file in map container and printing the contents of map

Hi everyone
I am trying to store the elements of text file in map container. The elements of text file are not fixed number. I write this code to do this task but when I am trying to print the content of map, nothing appear. Also, the size of map is always 1. I do not know if this code correct or not and what is the problem to store the contents of file in map and display the content of map. Anyone can help me please.
Regards


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  Put the code you need help with here.
[[code]#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <sstream>
#include <cmath>
#include <map>
#include <iterator>
using namespace std;

int main()
{
 map<double_t,double_t> arrivalmap; 
map<double_t,double_t>::iterator it;
int pos,pos2,pos3;
string line,search,word1="ns",word2="Pkt_create", word3="Pkt_recv";  
ifstream inf;  
inf.open ("123.txt");  
if (inf.is_open())  
{   
	while (getline(inf,line)) // loop continue until reaching the end of file.   
	{      
	    
   		if (line.find("Pkt_recv") != string::npos)   // search on "search" each line 
		{  
		pos=line.find(word1); // take the length of "ns" in string line.
		pos3=line.find(word3);// take the length of "Pkt_recv" in string line.
		line.replace(pos,word1.length(),"  "); // to remove "ns"
		line.replace(pos3,word3.length(),"  "); // to remove "Pkt_recv" 
        istringstream iss (line);
        double_t column1;
        //double_t column3;
        double_t column2;
        iss >> column1 >> column2;
        cout << column1 <<"  "<< column2 <<"  "<< " "<< endl;
	    map<double_t,double_t> arrivalmap; 
	    arrivalmap[column1]=column2;
	    for (map<double_t,double_t>::iterator it = arrivalmap.begin(); it != arrivalmap.end(); it++)
	    {  
	     cout << it->first <<"  "<< it->second << endl;
	     
	     } 
	     cout << arrivalmap.size()<<endl;

	    }

	 

}	    


inf.close();
	
}

else cout << "Unable to open file inf \n";	    
	

return 0;



}
]
Last edited on
Your problem is probably the second declaration of arrivalmap. When you add to your map, you're adding to the version scoped within the if statement, not the one scoped in main.

Remember that variable declarations with the same identifier in different scopes is legal in C++.

https://ideone.com/2eKKyh
Last edited on
Thanks for your reply
I did not notice that.
The code is working correctly.
Cheers
Topic archived. No new replies allowed.