Skip to content

Instantly share code, notes, and snippets.

@jflopezfernandez
Last active May 7, 2018 13:26
Show Gist options
  • Save jflopezfernandez/93c5223d0220ab3e4736c49a0fa5e0a6 to your computer and use it in GitHub Desktop.
Save jflopezfernandez/93c5223d0220ab3e4736c49a0fa5e0a6 to your computer and use it in GitHub Desktop.
Read text file character by character in C++
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
/** Description:
* Replacement for std::endl. Prints new line without
* flushing the output buffer.
*
*/
template <typename T, typename Traits = char_traits<T>>
std::basic_ostream<T, Traits>&
NL(std::basic_ostream<T, Traits>& out)
{
return out << out.widen('\n');
}
int main()
{
std::ifstream inputFile;
inputFile.open("main.c", std::ios::in);
if (!inputFile.is_open())
{
std::cerr << "[Error]: Failed to open input file..." << NL;
return EXIT_FAILURE;
}
char input = 0;
while ((input = inputFile.get()) != EOF)
{
std::cout << input;
}
inputFile.close();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment