Users Online

· Guests Online: 118

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

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.

  1. /*
  2.  * C++ Program to Print Lines of a File from Bottom to Top
  3.  */
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7.  
  8. int main()
  9. {
  10.     std::string line;
  11.     std::vector<std::string> v;
  12.     std::ifstream file("main.cpp");
  13.  
  14.     while (getline(file, line))
  15.     {
  16.         v.push_back(line);
  17.     }
  18.     /* Printing the lines from Bottom to Top */
  19.     for (int i = v.size() - 1; i >= 0; i--)
  20.     {
  21.         std::cout << v[i] << std::endl;
  22.     }
  23.     return 0;
  24. }

 

$ 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.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.13 seconds
10,267,279 unique visits