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 /*
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Check C++ Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Check Programming Books