Skip to main content

C++ Program to implement File Handling by using ifstream & ofstream -2

Write a paragraph (At least 2 lines of sentences) in a file. Read the same file in three different ways –
  i) Reverse the whole paragraph.
 ii) Line by Line Reverses the whole para.
iii) Break the paragraph into two equal halves. Reverse the second half of the paragraph.




#include
#include <fstream.h>
#include <conio.h>

void main(){
    char para[500], ln[80];
    int len, i, half;
    clrscr();

    cout<<"\n Enter a Paragraph (Quit by ^Z):-";
    cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    cin.getline(para, 500, '^Z');
    len = strlen(para);

    ofstream fout;
    fout.open("para.txt", ios::out);
    fout.write((char *) &para, len);
    fout.close();

    ifstream fin;
    cout<<"\n Reverse the Whole Paragraph:-";
    cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    fin.open("para.txt", ios::in);
    fin.read((char *) &para, sizeof(para));
    len = strlen(para);
    for(i=len; i>=0; i--)
        cout<    fin.close();
    getch();

    cout<<"\n Line by line Reverse the Whole Paragraph:-";
    cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    fin.open("para.txt", ios::in);
    while(fin){
        fin.getline(ln, 80);
        len = strlen(ln);
        for(i=len; i>=0; i--)
            cout<<ln[i];
        cout<<endl;
        }
    fin.close();
    getch();

    cout<<"\n Breaking The Para Into 2 halves and Reversing the 2nd Para:-";
    cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    fin.open("para.txt", ios::in);
    fin.read((char *) &para, sizeof(para));
    len = strlen(para);
    half = len / 2;
    for(i=0; i<=half; i++)
        cout<    for(i=len; i>half; i--)
        cout<    fin.close();
    getch();
    }

Comments