Reads a text file and display it backwards in the console.
g++ main.cpp -o backward.exe
#include <iostream> | |
#include <fstream> | |
#include <string> | |
int main(int argc, char **argv) | |
{ | |
std::ifstream file("HelloWorld.txt"); | |
std::string words; | |
if (file.is_open()) { | |
std::string temp; | |
while (std::getline(file, temp)) { | |
words += temp; | |
words.push_back('\n'); | |
} | |
} | |
for(int i = 0; i < words.length() / 2; ++i) { | |
char temp = words[i]; | |
words[i] = words[words.length() - (i + 1)]; | |
words[words.length() - (i + 1)] = temp; | |
} | |
std::cout<<words<<std::endl; | |
return 0; | |
} |