Read the content of a file

I was wondering how to read the content of a file and output it in the console.

I was trying to read it, but I can only get it to display the name of the file.
You can read the file line by line and print each line
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <fstream>

int main()
{
	std::ifstream file("filename");
	std::string line;
	while (std::getline(file, line))
	{
		std::cout << line << std::endl;
	}
}


This also seems to work but don't ask me to explain this solution.
1
2
3
4
5
6
7
8
#include <iostream>
#include <fstream>

int main()
{	
	std::ifstream file("filename");
	std::cout << file.rdbuf();
}
Topic archived. No new replies allowed.