Sie sind auf Seite 1von 3

/* Opening & closing files Example: InfoBrother:*/

#include<iostream>//Required for input & output:


#include<fstream>//Required for File function:
using namespace std;

int main()
{
/*Create New file: in just Write Mode:*/
fstream myFile;
cout<<"WRITING PURPOSE:"<<endl;
myFile.open("InfoBrother.txt",ios::out); //open file in writing mode:

if(!myFile) //error checker: if file did'nt open:


{
cout<<"Error while Opening File: "<<endl;
}
else cout<<"New File Open Successfully for Writing Purpose:"<<endl;

myFile.close(); //close the file:


if(myFile.is_open()==NULL) //error checker, if file still open:
{
cout<<"File has been Closed" <<endl;
}
else cout<<" File still Open: "<<endl;

/*Open Existing File for Reading and Appending Purpose:*/


cout<<"\n\nREADING & APPENDING PURPOSE: "<<endl;
myFile.open("InfoBrother.txt", ios::in | ios::app); //file Opening:

if(!myFile)
{
cout<<"Error While Opening File: "<<endl;
}
else cout<<" Existing File Open Successfully for Reading & Appending
Purpose: "<<endl;

//Let's dont close the file and check the error:


if(myFile.is_open()==NULL)
{
cout<<" File has been closed: "<<endl;
}
else cout<<"File still Open: "<<endl;

return 0;
} Closing File:

Reading From Files:


/* Reading from Sequential files: InfoBrother:*/

#include<iostream>//Required for input & output:


#include<fstream>//Required for File function:
using namespace std;
int main()
{
ifstream myFile; //object of type ifstream:
char fileName[20]; //will use to store fileName from user:
char ch; //will use to read all data from file:

cout<<" Enter the Name of the file you want to read: "<<endl;
cin>>fileName;

myFile.open(fileName, ios::in); //open file for reading purpose:

if(!myFile) //error checker: if file not exist:


{
cout<<"Error While Opening File: "<<endl;
}

else //if file exists and open successfully:


{
cout<<"File Open Successfully: here is the result: "<<endl;
while(!myFile.eof()) //read till end-of-file:
{
myFile.get(ch); //read data from file:
cout<<ch; //display data on screen:
}
}

myFile.close(); //close the file:

return 0;
}
Copy and writing
/*Copying File Program: InfoBrother*/

#include<fstream>//required for file operation:


#include<iostream>//required for input/output:
using namespace std;

main()
{
char File1[20],File2[20],ch; //variable to store file-Names
ifstream in_file; //Object for Input file
ofstream out_file; //Object for Output file

cout<<" Enter The Name Of File To be Copy: \n";


cout<<" File Name:";
cin>>File1;
cout<<" Enter The Name of File Where To copy: \n";
cout<<" File Name:";
cin>>File2;

in_file.open(File1,ios::in); //file Open for reading purpose: (copy)


if(!in_file) //Error Checker: if file not open:
{
cout<<"An Error Occur While Opening File: \n";
}

out_file.open(File2,ios::app); //file Open for writing purpose: (paste)


if(!out_file) //Error Checker: if file not open:
{
cout<<"An Error Occur While Opening Filef: \n";
}

while (in_file.get(ch)) //read data from file: (copy data)


{
out_file.put(ch); //paste it here:
}

in_file.close(); //closing files:


out_file.close();
cout<<" \n\n Copy Successful...!! : \n";

return 0;
}

Das könnte Ihnen auch gefallen