Reading a file and splitting lines into commas then push back into vector.

// I want to read from a file and split each line by comma
// after splitting push those words into a vector and then display vector.
// I tried this code below but did not work as I expected. My text file is
// given under the code.
// My expected output is this
// Hamidur
// Rahman
// MAC
// 125
// MAC
// 190
// MAT
// 231
// Thanks advance.

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

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

int main ( )
{
	string line;

	int i = 0;

	vector < string > v;

	ifstream fin;

	fin.open( ( "file.txt" ) );

	if ( fin.is_open())
	{
		while ( getline ( fin, line ))
		{
			stringstream ss ( line );

			if ( getline ( ss, line, ','))
			{
				v.push_back( line );
			}
		}

		while ( v.size() )
		{
			cout << v [ i ] << "\n";
			i++;
		}
	}
}


// file.txt has those lines
// Hamidur,Rahman
// MAC,125
// MAC,190
// MAT,231
Last edited on
did not work as I expected.

Describe what did happen.


Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post.
I see two problems.
1
2
3
4
if ( getline ( ss, line, ','))
{
  v.push_back( line );
}
You read only the part in front of the comma.

1
2
3
4
5
while ( v.size() )
{
  cout << v [ i ] << "\n";
  i++;
}

That's not the proper way to display vector.
while ( v.size() ) will always be true.
Once i is 4 it will cause an out_of_range exception.
@Thomas1965
How do I read parts that are after a comma.

@keskiverto
Thanks. I actually did not know that. I changed it.
Last edited on
Read 2 strings:
1
2
3
4
5
6
7
8
9
  string line = "Anna-Maria,Schmidt";
  stringstream ss(line);
  string firstname;
  getline(ss, firstname, ',');
  string lastname;
  getline(ss, lastname);

  cout << "First name = " << firstname << "\n"
       << "Last name = " << lastname; 


Read a string and a number
1
2
3
4
5
6
7
8
9
10
  string line = "Anna Schmidt,22";
  stringstream ss(line);
  string firstname;
  getline(ss, firstname, ',');
  int age;
  ss >> age;

  cout << "Name = " << firstname << "\n"
       << "Age = " << age; 
I have to disagree with Thomas about the "first problem" -- it is not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <sstream>

int main ()
{
  std::string line = "foo,bar,gaz";
  std::stringstream ss(line);
  std::string name;
  while ( getline( ss, name, ',') ) {
    std::cout << "# " << name << " #\n";
  }
  return 0;
}

# foo #
# bar #
# gaz #

The getline ( ss, line, ',') reads up to a comma or end-of-stream, whichever comes first. The word after the last comma is a valid read.


The real issue is the way the data is printed from the vector, like Thomas said.

C++11 has a ranged for syntax that is ideal for this purpose.
A traditional loop is okay too, if written correctly.


I did ask you to describe what did happen, because one can usually realize the problem self when trying to tell it to others. That is important part of programming.
Last edited on
I finally figure it out.
Thanks everyone for your time.


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
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main ( )
{
	string line;

	int i = 0;

	vector < string > v;

	ifstream fin;

	fin.open ("file.txt");

	if ( fin.is_open ( ))
	{
		while ( getline ( fin, line ))
		{
			stringstream ss ( line );

			while ( getline (ss ,line, ','))
			{
				v.push_back(line);
			}
		}

		while ( true )
		{
			if ( i == v.size())
			{
				break;
			}

			cout << v [ i ] << "\n";
			i++;
		}
	}
}

Topic archived. No new replies allowed.