Read series name and write in reverse order using I/O redirection and pipes in C++ Programming Language

PROGRAM STATEMENT

Write a C++ program to read a series of names, one per line, from standard input and write these names spelt in reverse order to the standard output using I/O redirection and pipes. Repeat the exercise using an input file specified by the user instead of the standard input and using an output file specified by the user instead of the standard output.

Objective

In C++, there are functions like ‘ofstream‘ and ‘ifsteam’ which perform a single task of writing (output file steam) and reading (input file steam) into the files. Similarly, fstream‘ is able to handle both using a single function. So, here we will be including ‘fstream‘ header for file-related functions.

First, we will get the series of the name from the standard input and file and then we will write it in the reverse order. Basically, the overall concept is using basic file operations (i.e. reading and writing to the file) and usage of redirection and pipes in the command line.

Coding approach

At first, we will ask the user to enter a string. This will be our Standard Input.

And we will present options such as the followings:

  1. Standard Reverse
  2. FIle Reverse
  3. Exit

And depending on the user’s choice the code will execute the output of the respective function.

Code for basic file operation and redirection & pipes in the command line

/* Function input:in.txt Function output:out.txt
*/
#include
#include // Header for file related functions #include
using namespace std;
//Function to reverse names from standard input console void reverse_std(const string& str)
{
size_t n = str.size(); if(n == 1)
cout << str << endl; else
{
cout << str[n - 1]; reverse_std(str.substr(0, n - 1));
} }
//Function to reverse names from a file int reverse_file()
{
int i,j;
string buffer;
fstream infile,outfile;
char tmp; infile.open("in.txt",ios::in); outfile.open("out.txt",ios::out); while(!infile.eof())
{
getline(infile,buffer); int len; len=buffer.size(); for(i=0;i<len/2;i++)
{
tmp=buffer[i];
buffer[i]=buffer[len-i-1]; buffer[len-i-1]=tmp;
} outfile<<buffer; outfile<<endl;
}
infile.close(); outfile.close(); return 0;
}
int main() {
int ch;
string str;
cout << "Please enter a string (Standard I/P): "; // Reading names from console getline(cin, str);
cout<<"1.Standard Reverse\n2.File Reverse\n3.Exit\n";
cout<<"Enter your choice: "; cin>>ch;
switch(ch) {
case 1: reverse_std(str); break;
case 2: reverse_file(); break;
case 3: cout<<"Exiting Program."; exit(0);
default:cout<<"Enter a valid Choice.";
} }

OUTPUT

 Output of C++ Function to reverse names from standard input console


The output of C++ Function to reverse names from standard input console

Input in.txt of Code for basic file operation and redirection & pipes in the command line

Input in.txt of Code for basic file operation and redirection & pipes in the command line

Output out.txt of Code for basic file operation and redirection & pipes in the command line

Output out.txt of Code for basic file operation and redirection & pipes in the command line

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Leave a Reply

Your email address will not be published. Required fields are marked *