File Handling in C++ : Programs

********************************PROGRAM 1*****************************

//Creating a new file and writing contents in file
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 ofstream fout("try.txt");
 if(!fout)
 {
 cout<<"Error opening file";
 exit(1);
 }
 fout <<"hello";
 fout <<"hi";
 fout <<"bye";
 fout.close();
 return 0;
 }

********************************PROGRAM 2*****************************

//Creating a new file using open method and writing values of variables
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 string name,address;
 int rollno;
 cout<<"Enter your name, address and rollno";
 cin>>name>>address>>rollno;
 ofstream fout;
 fout.open("details.txt");
 if(!fout)
 {
 cout<<"Error opening file";
 exit(1);
 }
 fout<<name<<endl;
 fout<<address<<endl;
 fout<<rollno<<endl;
 fout.close();
 return 0;
 }

********************************PROGRAM 3*****************************

//opening a file in read only mode and reading contents of file
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 string str;
 ifstream fin("try.txt");
 if(!fin)
 {
 cout<<"Error opening file";
 exit(1);
 }
 cout<<"Contents in file : \n";
 fin >> str;
 cout<< str<<endl;
 fin >> str;
 cout<< str<<endl;
 fin >> str;
 cout<< str<<endl;
 fin.close();
 return 0;
 }

********************************PROGRAM 4*****************************

//opening a file in read only mode using open method and reading contents in variables
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 ifstream fin("details.txt");
 if(!fin)
 {
 cout<<"Error opening file";
 exit(1);
 }
 cout<<"Contents in file : \n";
 string name,address;
 int rollno;
 fin >> name;
 fin >> address;
 fin >> rollno;
 cout<<"Name:"<<name<<endl;
 cout<< "Address:"<<address<<endl;
 cout<< "Roll no:"<<rollno<<endl;
 fin.close();
 return 0;
 }

********************************PROGRAM 5*****************************

//copying contents of one file into other
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 char infile[80],outfile[80];
 cout<<"Enter name of file to copy:\n";
 cin>>infile;
 cout<<"Enter name of new file to save:\n";
 cin>>outfile;
 ifstream fin;
 fin.open(infile);
 if(!fin)
 {
 cout<<"Error opening file";
 exit(1);
 }
 ofstring fout;
 fout.open(outfile);
 if(!fout)
 {
 cout<<"Error opening file";
 exit(1);
 }
 cout<<"Copying contents of one file into other :\n";
 string data;
 while(fin)
 {
 getline(fin,data);
 fout<<data<<endl;
 }
 fin.close();
 fout.close();

cout<<"Copy Successful\n";

return 0;
 }

********************************PROGRAM 6*****************************

//use of fstream class to read contents of multiple files one by one
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 char infile[80];
 fstream fptr;
 do
 {
 cout<<"Enter name of file to read:\n";
 cin>>infile;
 fptr.open(infile,"ios::in");
 if(!fptr)
 {
 cout<<"Error opening file for reading";
 exit(1);
 }
 string data;
 while(fptr)
 {
 getline(fptr,data);
 cout<<data<<endl;
 }
 fptr.close();
 cout<<"Do you want to open other file for reading(y/n)\n";
 char ch;
 cin>>ch;
 }while(ch =='y');
 return 0;
 }

********************************PROGRAM 7*****************************

//use of fstream class to open existing file using file mode
 #include <iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 char filename[80];
 fstream fptr;
 cout<<"Enter name of file to write:\n";
 cin>>filename;
 fptr.open(filename,"ios::out|ios::app|ios::ate");
 if(!fptr)
 {
 cout<<"Error opening file for reading";
 exit(1);
 }
 cout<<"Enter contents of file : ^D to end \n";
 string data;
 while(getline(cin,data))
 {
 if(data == "^D")
 break;
 fptr << data << endl;
 }

fptr.close();
 return 0;
 }

********************************PROGRAM 8 *****************************

//write contents in two different files and display contents on terminal and merge two file in third file
 #include<iostream>
 #include<fstream>
 #include<cstdlib>
 using namespace std;
 int main()
 {
 //Creating first file for writing
 cout<<"Enter name of first file to create\n";
 char  name1[80],name2[80],name3[80];
 cin>>name1;
 fstream file;
 file.open(name1,ios::out);
 if(!file)
 {
 cout<<"Error creating file\n";
 exit(1);
 }
 cout<<"Enter contents of file ending with bye";
 string data;
 while(getline(cin,data))
 {
 if(data=="bye")
 break;
 file << data<<endl;
 }
 file.close();
 //Creating second file for writing
 cout<<"Enter name of second file to create\n";
 cin>>name2;
 file.open(name2,ios::out);
 if(!file)
 {
 cout<<"Error creating file\n";
 exit(1);
 }
 cout<<"Enter contents of file ending with bye";
 while(getline(cin,data))
 {
 if(data=="bye")
 break;
 file << data<<endl;
 }
 file.close();
 //Creating third file for merging
 cout<<"Enter name of third file to merge\n";
 cin>>name3;
 file.open(name3,ios::out); //file for writing
 fstream infile;//file pointer for reading
 infile.open(name1,ios::in);
 if(!file || !infile)
 {
 cout<<"Error opening file\n";
 exit(1);
 }
 while(infile)
 {
 getline(infile,data);
 file<<data<<endl;
 cout<<data<<endl;
 }
 infile.close();
 file.close();
 file.open(name3,ios::out|ios::app); //file for writing
 fstream infile;//file pointer for reading
 infile.open(name2,ios::in);
 if(!file || !infile)
 {
 cout<<"Error opening file\n";
 exit(1);
 }
 while(infile)
 {
 getline(infile,data);
 file<<data<<endl;
 cout<<data<<endl;
 }
 infile.close();
 file.close();
 return 0;
 }