Loading Tab delimited File in 2D vector string

Hi, This is my code to Read Tab delimited data from file and Load it into a 2D vector String

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
vector <vector <string> > indexTable;
int rowIndex,fieldIndex,rowSize;
int rowIndexCount, colIndexCount;

void LoadIndexTable(string fName)
{

        fstream fsIndex;

        fsIndex.open(fName.c_str(), ios::in);

	indexTable.clear();
	
	int startIndex,endIndex,fieldLength;
	
	rowIndex=0;
	
	rowSize=0;
	
	while (fsIndex)
	
	{
		
		string s;
		
		fieldIndex=0;
		
		if (!getline( fsIndex, s )) break;

		istringstream ss( s );
		
		vector <string> record;

		while (ss)
		
		{
			
			string s;
			
			if (!getline( ss, s, '\t' )) break;
			
				record.push_back( s );
      
			if(fieldIndex==2)
				startIndex= atoi(s.c_str());
				
			else if(fieldIndex==3)
				endIndex= atoi(s.c_str());
			
			else if(fieldIndex==6)
			{
				
				if(s.compare("pk")==0)
				
					primaryField=rowIndex;
					
				else if(s.compare("sk")==0)
				
					secondaryField=rowIndex;
			
			}	
			
			fieldIndex++;
			
		}
		
		std::stringstream fLenStream;
		
		fieldLength = endIndex-startIndex+1;
		
		fLenStream << fieldLength;
		
		
		record.push_back( fLenStream.str() );
		
		indexTable.push_back( record );
		
		record.clear();
		
		rowSize+=fieldLength;
		
		rowIndex++;
	}
	 
	rowIndexCount=indexTable.size();
		
	colIndexCount=indexTable[0].size()- 1;
	
}


After Reading the file I used to print the vector using iterators.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector< vector<string> >::iterator iter_ii;
	vector<string>::iterator   iter_jj;

for(iter_ii=ppp.indexTable.begin(); iter_ii!=ppp.indexTable.end(); iter_ii++)
	
	{
		
      for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++)
      
      {
		  
         cout << *iter_jj<<"\t";
         
      }
      
      cout<<endl;
      
   }


This is my First my file data.(file1.txt)

exdate ExDate 0 7 date c pk notnull
symbol Symbol 8 20 string c sk notnull
factor Factor 21 36 number c no null


This is my Second my file data.(file2.txt)

name Name 0 3 string c pk notnull
symbol Symbol 4 15 string c sk notnull
class Class 16 20 string c no notnull


In my load Function (LoadIndexTable()) I externally calculated every field size and I pushed it into 2D vector .

Now Let me say my problem. After reading file1.txt, I'm getting the below result. Its giving correct output.

exdate	ExDate	0	7	date	c	pk	notnull	8	
symbol	Symbol	8	20	string	c	sk	notnull	13	
factor	Factor	21	36	number	c	no	null	16	


After Reading file2.txt, I'm getting the result not as same ( Wrong at 2nd field ) .This is my output for file2.txt

name	4ame	0	3	string	c	pk	notnull
symbol	12mbol	4	15	string	c	sk	notnull
class	5lass	16	20	string	c	no	notnull


That is the field length which I calculated by my programming is overlapped in my 2nd Field of the 2D vector string. I don't know really why I'm getting this bug. Any one please explain me How to fix this bug.
Last edited on
At a cursory glance, there are a few things you should clean up. In your while loops, you declare a few variables. Since it is a loop, this means that it will declare them multiple times, causing an error. Additionally, you have declared the variable "string s" twice, once in each loop. The variables in question are in bold below:

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
	while (fsIndex)
	
	{
		
		string s; // This is also declared twice, in each loop
		
		fieldIndex=0;
		
		if (!getline( fsIndex, s )) break;

		istringstream ss( s );
		
		vector <string> record;

		while (ss)
		
		{
			
			string s; // This is also declared twice, in each loop
			
			if (!getline( ss, s, '\t' )) break;
			
				record.push_back( s );
      
			if(fieldIndex==2)
				startIndex= atoi(s.c_str());
				
			else if(fieldIndex==3)
				endIndex= atoi(s.c_str());
			
			else if(fieldIndex==6)
			{
				
				if(s.compare("pk")==0)
				
					primaryField=rowIndex;
					
				else if(s.compare("sk")==0)
				
					secondaryField=rowIndex;
			
			}	
			
			fieldIndex++;
			
		}
		
		std::stringstream fLenStream;
		
		fieldLength = endIndex-startIndex+1;
		
		fLenStream << fieldLength;
		
		
		record.push_back( fLenStream.str() );
		
		indexTable.push_back( record );
		
		record.clear();
		
		rowSize+=fieldLength;
		
		rowIndex++;
	}

Last edited on
I changed my function as Below. But still I'm getting the same bug.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
void LoadIndexTable(string fName)
{

        fstream fsIndex;

        fsIndex.open(fName.c_str(), ios::in);

	indexTable.clear();
	
	int startIndex,endIndex,fieldLength;
	
	rowIndex=0;
	
	rowSize=0;
	
	std::stringstream fLenStream;

	string s,rec;
	
	vector <string> record;
	
	while (fsIndex)
	
	{
		
		fieldIndex=0;
		
		if (!getline( fsIndex, s )) break;

		istringstream ss( s );
		
		while (ss)
		
		{
			
			if (!getline( ss, rec, '\t' )) break;
			
				record.push_back( rec );
				
			if(fieldIndex==2)
				startIndex= atoi(rec.c_str());
				
			else if(fieldIndex==3)
				endIndex= atoi(rec.c_str());
			
			else if(fieldIndex==6)
			{
				
				if(rec.compare("pk")==0)
				
					primaryField=rowIndex;
					
				else if(rec.compare("sk")==0)
				
					secondaryField=rowIndex;
			
			}	
			
			fieldIndex++;
			
		}
		
		fieldLength = endIndex-startIndex+1;
		
		fLenStream << fieldLength;
		
		
		record.push_back( fLenStream.str() );
		
		indexTable.push_back( record );
		
		rowSize+=fieldLength;
		
		rowIndex++;
		
		record.clear();
		
		s.erase();
		
		rec.erase();
		
		fLenStream.str("");
		
	}
	 
	rowIndexCount=indexTable.size();
		
	colIndexCount=indexTable[0].size()- 1;
	
}
My 2D vector got collapsing after reaching this statement.

indexTable.push_back( record );
Topic archived. No new replies allowed.