#include <fstream>
#include <iostream>
int main()
{
char fileName[80];
//char buffer[255];
std::cout<<"Enter the filename to open.\n";
std::cin>>fileName;
//std::ofstream fout(fileName);
//fout << "This line is written directly to the file.\n";
//std::cout <<"Enter the text for the file:";
//std::cin.ignore(1,'\n');
//std::cin.getline(buffer,255);
//fout << buffer <<"\n";
//fout.close();
std::ifstream fin(fileName);
std::cout<<"Heres the contents of the file:\n";
char ch;
while (fin.get(ch))
std::cout << ch;
std::cout<<"\n***End of file contents***\n";
fin.close();
}
By default, formatted input with >> ignores white space.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <fstream>
int main()
{
std::ifstream file( __FILE__ ) ; // this file
char c ;
while( file >> c ) std::cout << c ; // read and print all non-space characters
std::cout << '\n' ; // put a new line at the end (just being well mannered).
}
Use a modern compiler that is compiling the program to one of the modern C++ standards, C++11, C++14, or C++17.
Depending on the compiler you're using you may need to inform it to use one of those modern standards by modifying the compile options. See the documentation for your compiler and IDE for more information.