Users Online
· Guests Online: 118
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Print the Contents of File in Reverse Order
C++ Program to Print the Contents of File in Reverse Order
This C++ Program which prints the lines of a file from bottom to top. The program creates an input file stream, reads a line on every iteration of a while loop and saves every line in a string vector. The for loop prints the lines from the end of the vector to the start of it.
Here is source code of the C++ program which prints the lines of a file from bottom to top. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
-
/*
-
* C++ Program to Print Lines of a File from Bottom to Top
-
*/
-
#include <iostream>
-
#include <fstream>
-
#include <vector>
-
-
int main()
-
{
-
std::string line;
-
std::vector<std::string> v;
-
std::ifstream file("main.cpp");
-
-
while (getline(file, line))
-
{
-
v.push_back(line);
-
}
-
/* Printing the lines from Bottom to Top */
-
for (int i = v.size() - 1; i >= 0; i--)
-
{
-
std::cout << v[i] << std::endl;
-
}
-
return 0;
-
}
$ g++ main.cpp $ ./a.out } } std::cout << v[i] << std::endl; { for (int i = v.size() - 1; i >= 0; i--) } v.push_back(line); { while (getline(file, line)) std::ifstream file("main.cpp"); std::vector<std::string> v; std::string line; int count = 0; { int main() #include <vector> #include <fstream> #include <iostream> */ * C++ Program to Print Lines of a File from Bottom to Top /*
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.