How to read entire text file into a string in C++

1 Answer

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

using std::cout;
using std::endl;

int main()
{
	std::ifstream ifs("d:\\data.txt");

	std::string s((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));

	ifs.close();

	cout << s << endl;

	return 0;
}

/*
run:

c c++
c# java php python

*/

 



answered Jun 11, 2018 by avibootz
edited Jun 11, 2018 by avibootz

Related questions

1 answer 253 views
1 answer 305 views
1 answer 236 views
1 answer 325 views
5 answers 457 views
1 answer 262 views
...