Strange Vector problem

Hi
Please anyone check my code to see what is wrong with it.
I am stuck please help.
First i am parsing a csv file using boost tokenizer.
I have a test csv file that has

"AXISBANK HELLO.BO","AXIS BANK",1156.30,"4/2010",0.00,1167.00,1142.25,10.00,N/A

"IFCI.BO","IFCI LTD",53.25,"4/8/2010",0.00,53.50,51.10,0.80,N/A

Now my code.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef boost::tokenizer<boost::escaped_list_separator<char>> my_tokenizer;
	std::vector<std::string> mystr;
	std::string line;
	while(std::getline(inputfile,line))
	{

		my_tokenizer tok(line);
		int i=0;
			for (my_tokenizer::iterator it(tok.begin()),
			end(tok.end());
			it != end; ++it)
		{
		 
		 mystr.push_back(*it);
		 std::cout << (*it)<< std::endl;
		
	          std::cout.flush();

		}

The above code gives what you except every token i.e a word in the file except '"'. ',' and '\'. but now my problem is when i try to print the vector mystr i am not getting the last word "N/A" , The last element in the vector is empty.
Again my code ....
1
2
3
4
5
6
7
8
9
10
std::vector<std::string>::iterator mit=mystr.begin();
		  i=0;
		for(;mit!=mystr.end();mit++)
		{
			std::cout <<std::endl;
			std::cout << *mit;
			std::cout.flush();
			i++;
		}

The above code does not print the word "N/A". But the below code works fine.

 
copy(mystr.begin(), mystr.end(),std::ostream_iterator<std::string>(std::cout,""));

When i tried to view the Ninth element of the vector using the [] operator it does not print anything and if(mysrt[8]=="N/A") also failed.Please anyone guide me to solve the above problem.

The complete program
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
// boosttoken.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <boost/tokenizer.hpp>
#include <iterator>
#include <iostream>
#include <vector>


int _tmain(int argc, _TCHAR* argv[])
{
	std::ifstream inputfile("c:/sample.txt");
	
	if(!inputfile.is_open())
	{
		std::cout << "error oening file";
		return 1;
	}
	
typedef boost::tokenizer<boost::escaped_list_separator<char>> my_tokenizer;
	std::vector<std::string> mystr;
	std::string line;
	while(std::getline(inputfile,line))
	{

		my_tokenizer tok(line);
		
		int i=0;
			for (my_tokenizer::iterator it(tok.begin()),
			end(tok.end());
			it != end; ++it)
		{
		
		mystr.push_back(*it);
		// std::cout << (*it)<< std::endl;
		 std::cout <<"----"<< mystr[i++];
     	 std::cout.flush();

		}
       
		
	//	copy(mystr.begin(), mystr.end(),
	//		std::ostream_iterator<std::string>(std::cout, ""));

		 
		  
		
for(std::vector<std::string>::iterator mit(mystr.begin());mit!=mystr.end();mit++)
		{
			std::cout <<std::endl;
			std::cout << *mit;
			std::cout.flush();
			
		}
        
		mystr.clear();

		std::cout<<"A line completed "<<std::endl;
       


	}

	return 0;
}






With regards..........
Last edited on
Topic archived. No new replies allowed.