How to read words from text file into a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	vector<string> vec;
	
	std::ifstream in("d:\\data.txt");

	string word;
	while (in >> word)
		vec.push_back(word);

	in.close();
	
	for (string val : vec)
		cout << val << endl;

	return 0;
}

/*
run:

python
java
c#
php
c++

*/

 



answered May 7, 2018 by avibootz

Related questions

1 answer 182 views
1 answer 181 views
1 answer 162 views
1 answer 329 views
1 answer 249 views
1 answer 258 views
1 answer 250 views
...