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 245 views
1 answer 291 views
1 answer 226 views
1 answer 303 views
5 answers 431 views
1 answer 250 views
...