A menu driven program to create, append, read and copy a text file. Also include a function to read the file content in uppercase. The name of the original and copied file is entered by the user. The program should contain the following fuctions:
               write(), append(), read(), uppercase(), copy()

 

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

char a[100],b,file[30];

void write()
{
cout<<"\nEnter data to be entered in file: ";
gets(a);

ofstream file1;
file1.open(file,ios::out);

file1<<a;

file1.close();
}

void append()
{
cout<<"\nEnter content to be added: ";
gets(a);

ofstream file1;
file1.open(file,ios::app);

file1<<a;

file1.close();
}

void read()
{
ifstream file1;
file1.open(file,ios::in|ios::noreplace);

cout<<"\nContent of this file: ";

while(file1.get(b))
cout<<b;
cout<<endl;

file1.close();
}

void copy()
{
char d,copyfile[30];

cout<<"\nEnter name of copied file: ";
cin>>copyfile;

ofstream file2;
file2.open(copyfile,ios::out);

ifstream file1;
file1.open(file,ios::in);

while(file1.get(d))
file2.put(d);

file1.close();
file2.close();
}

void uppercase()
{
ofstream file1;
file1.open(file,ios::out) ;

while(file1.get(a))
cout<<toupper(a); 
cout<<endl;


file1.close();
}

void main()
{
 int ch;
 clrscr();
 cout<<"Enter file name: ";
 cin>>file;
 cout<<endl;

 do
  {
    cout<<"1. Change File name."<<endl;
    cout<<"2. Write on the file."<<endl;
    cout<<"3. Add contents to the file."<<endl;
    cout<<"4. Read file content."<<endl;
    cout<<"5. Read file content in uppercase."<<endl;
    cout<<"6. Copy the file."<<endl;
    cout<<"7. Exit."<<endl;
    cout<<"Enter your choice: ";
    cin>>ch;
    switch(ch)
     {
       case 1: {
                    cout<<"\nEnter File name: ";
                    cin>>file;
                    cout<<endl;
                    break;
                   }
       case 2: {
                    write();
                    break;
                   }
       case 3: {
                    append();
                    break;
                   }
       case 4: {
                    read();
                    break;
                   }
       case 5: {
                    uppercase();
                    break;
                   }
       case 6: {
                    copy();
                    break;
                   }
       case 7: {
                    break;
                   }
       default: {
                    cout<<"\nWrong choice entered.\n"<<endl;
                    break;
                   }
     }    

     cout<<endl;
  }
 while(ch!=7);
}

 

< Back