Last active
May 7, 2018 13:26
-
-
Save jflopezfernandez/93c5223d0220ab3e4736c49a0fa5e0a6 to your computer and use it in GitHub Desktop.
Read text file character by character in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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