Files and Streams
Files and Streams
Data Description
Type
#include <iostream>
#include <fstream>
int main()
{
fstream file; //object of fstream class
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
int main()
{
char ch;
const char *fileName="test.txt";
//declare object
ifstream file;
//open file
file.open(fileName,ios::in);
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return -1; //return from main
}
return 0;
}
int main()
{
fstream file; //object of fstream class
if(!file)
{
cout<<"Error in creating file!!!"<<endl;
return 0;
}
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return 0;
}
while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}
return 0;
}
int main()
{
char name[30];
int age;
fstream file;
file.open("aaa.txt",ios::out);
if(!file)
{
cout<<"Error in creating file.."<<endl;
return 0;
}
cout<<"\nFile created successfully."<<endl;
file.close();
cout<<"\nFile saved and closed succesfully."<<endl;
cout<<"Name: "<<name<<",Age:"<<age<<endl;
return 0;
}
void showData(void)
{
cout<<"Name:"<<name<<",Age:"<<age<<endl;
}
};
int main()
{
student s;
ofstream file;
return 0;
}
#include <iostream>
#include <fstream>
int main()
{
fstream F;
// opening a file in input and output mode
F.open("my.txt", ios::in | ios::out);
// seeing 8 bytes/characters
F.seekg(8, ios::beg);
// now, getting the current location
cout << F.tellg() << endl;
// extracting one character from current location
char c = F.get();
// printing the character
cout << c << endl;
// after getting the character,
// getting current location
cout << F.tellg() << endl;
// now, seeking 10 more bytes/characters
F.seekg(10, ios::cur);
// now, getting current location
cout << F.tellg() << endl;
// again, extracing the one character from current location
c = F.get();
// printing the character
cout << c << endl;
//global declaration
fstream file;
file.open("EMPLOYEE.DAT",ios::binary|ios::app);
if(!file){
cout<<"ERROR IN CREATING FILE\n";
return;
}
//write into file
file.write((char*)&x,sizeof(x));
file.close();
cout<<"Record added sucessfully.\n";
}
void displayAll(){
Employee x;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout<<"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x)))
if(x.getSalary()>=10000 && x.getSalary()<=20000)
x.display();
}
file.close();
}
void searchForRecord(){
//read employee id
Employee x;
int c;
int isFound=0;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout<<"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()==c){
cout<<"RECORD FOUND\n";
x.display();
isFound=1;
break;
}
}
}
if(isFound==0){
cout<<"Record not found!!!\n";
}
file.close();
}
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout<<"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()==c){
cout<<"Salary hike? ";
cin>>sal;
x.updateSalary(x.getSalary()+sal);
isFound=1;
break;
}
}
}
if(isFound==0){
cout<<"Record not found!!!\n";
}
file.close();
cout<<"Salary updated successfully."<<endl;
}
fstream fin;
//read file in input mode
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
//open file in write mode
fin.open("TEMP.DAT",ios::binary|ios::out);
if(!file){
cout<<"Error in opening EMPLOYEE.DAT file!!!\n";
return;
}
if(!fin){
cout<<"Error in opening TEMP.DAT file!!!\n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()>newEmp.getEmpCode()){
fin.write((char*)&newEmp, sizeof(newEmp));
}
//no need to use else
fin.write((char*)&x, sizeof(x));
}
}
fin.close();
file.close();
rename("TEMP.DAT","EMPLOYEE.DAT");
remove("TEMP.DAT");
cout<<"Record inserted successfully."<<endl;
}
int main()
{
char ch;
do{
int n;
cout<<"ENTER CHOICE\n"<<"1.ADD AN EMPLOYEE\n"<<"2.DISPLAY\
n"<<"3.SEARCH\n"<<"4.INCREASE SALARY\n"<<"5.INSERT RECORD\n";
cout<<"Make a choice: ";
cin>>n;
switch(n){
case 1:
appendToFille();
break;
case 2 :
displayAll();
break;
case 3:
searchForRecord();
break;
case 4:
increaseSalary();
break;
case 5:
insertRecord();
break;
default :
cout<<"Invalid Choice\n";
}
}while(ch=='Y'||ch=='y');
return 0;
}
int main()
{
ifstream fin;
ofstream fout;
fin.open("alex.txt",ios::in);
fout.open("new.txt",ios::out);
if(!fin||!fout){
cout<<"ERROR\n";
return -1;
}
char ch;
while(fin)
{
if(fin.get(ch))
{
if(isupper(ch))
ch+=32;
else if(islower(ch))
ch-=32;
}
fout.put(ch);
}
fin.close();
fout.close();
//print the content of net.txt
fin.open("new.txt",ios::in);
if(!fin){
cout<<"Error";
return -1;
}
cout<<"Content of new.txt file :\n";
while(fin){
if(fin.get(ch))
cout<<ch;
}
cout<<endl;
fin.close();
///////////////////////////////
return 0;
}