There are many projects about management system on net but some are very simple and some very difficult to understand......so i write this project in very simple and easy way...
This project is very simple and useful for new learning students........ hope so you could be the benifitiories of this project....
// Haroon.cpp :
Defines the entry point for the console application.
//
#include "stdafx.h"
#include "conio.h"
#include "process.h"
#include "iostream"
#include "fstream"
using namespace std;
struct organization
{
char
name[20],fathername[20],address[30],Designation[10];
int worker_no,age,salary;
};
void main()
{
int choice,temp,status =
0;
char again; // used in do-while loop for moving back to menu
organization o;
ofstream infile; // declaring file handle for write only
fstream file; // declaring file handle for both input and output
do{
system("color
f3");
system("cls");
cout<<"
===============================================\n";
cout<<" | Main Menu of Our Organization |\n";
cout<<"
===============================================\n";
cout<<"=============================\n";
cout<<"= 1- Add Worker ="<<endl;
cout<<"= 2- Delete Worker ="<<endl; /// menu
cout<<"= 3- Edit Worker ="<<endl;
cout<<"= 4- List of all workers ="<<endl;
cout<<"=============================";
cout<<"\n\nenter your choice>> ";
cin>>choice;
switch(choice)
{
case 1:
infile.open("organization.txt",ios::app | ios::out);
// opening file to write
if(!infile) // checking file is opened or not
{
cout<<"Can't open file"<<endl;
system("pause");
exit(0); //
closing program
}
cout<<endl<<"Enter name of Worker: ";
cin>>o.name;
cout<<"Father name: ";
cin>>o.fathername;
cout<<"age: ";
cin>>o.age;
cout<<"Address: ";
cin>>o.address;
cout<<"worker_no: ";
cin>>o.worker_no;
cout<<"Designation: ";
cin>>o.Designation;
cout<<"salary: ";
cin>>o.salary;
infile.seekp(ios::end);
infile.write((char*)&o,sizeof(o));
//writing data to file(organization.txt)
infile.close(); // closing organization.txt
break;
case 2: //case written to delete record of a worker
file.open("organization.txt",ios::in);
// opening file in read mode only
if(!file) // checking opening of file
{
cout<<"can't open file"<<endl;
system("pause");
exit(0);
}
infile.open("temp.txt",ios::app | ios::out); // opening file for writing
// but writing can
only be done at end
// creating temp file
used in deletion of record
if(!infile)
{
cout<<"Can't open file"<<endl;
system("pause");
exit(0);
}
cout<<"Enter Worker_NO of Worker ";
cin>>temp;
while(file.read((char*)&o,sizeof(o)))
// loop will iterate untill end of file
//and also load data
from file
{
if(temp != o.worker_no)
{
infile.write((char*)&o,sizeof(o));
// writing in temp.txt
status = 1; // setting status to 1 which means that vlaue exist
// loop will word
untill end of file and copies all record
// and copies all
records to temp file and this if portion will
// be skipped beacuse
(temp != s.roll_no) when record to delete
// and data in file
are same then writing in temp file will
// does not take
place.
}
}
file.close(); // closing both organization.txt and temp.txt
infile.close();
remove("organization.txt");
// deleting organization.txt
rename("temp.txt","organization.txt"); // renaming temp.txt as organization.txt
if(status == 0) // if status is 0 then value does not exist
{
cout<<"Worker with thsi NO. does not exists in our
Organization"<<endl;
system("pause");
}
break;
case 3: //case written for
editing
cout<<"Enter Worker_no to modifiy the record of worker>>>
";
cin>>temp;
file.open("organization.txt",ios::in
| ios:: out); //opening file for both input and out
put
while(file.read((char*)&o,sizeof(o)))
{
if(temp == o.worker_no) //
comparing data in file with user entered value
{
cout<<"\t<<<<<<Old Information of
Worker>>>>>>"<<endl; // printing old information resident in file
cout<<"Name: "<<o.name<<endl;
cout<<"Father Name: "<<o.fathername<<endl;
cout<<"Age: "<<o.age<<endl;
cout<<"Address: "<<o.address<<endl;
cout<<"Worker_no "<<o.worker_no<<endl;
cout<<"Designation: "<<o.Designation<<endl;
cout<<"salary: "<<o.salary<<endl<<endl;
cout<<endl<<"\t<<<<<<Enter new
record>>>>>>"<<endl;
cout<<endl<<"Enter name of Worker: ";
cin>>o.name;
cout<<"Father name: ";
cin>>o.fathername;
cout<<"age: ";
cin>>o.age;
cout<<"Address: ";
cin>>o.address;
cout<<"worker_no: ";
cin>>o.worker_no;
cout<<"Designation: ";
cin>>o.Designation;
cout<<"salary: ";
cin>>o.salary;
int location = 0; // used to store pointer of file where pointer i
// file at present
time
location = -1*sizeof(o); // storing no of
bytes that a record takes to store
file.seekp(location,ios::cur); // moving pointer in file back one record
file.write((char*)&o,sizeof(o));
// writing to the same file again updated value
status = 1;
break;
}
} //
while ending
if(status == 0)
{
cout<<"Worker with this NO. does not exists in our
Organization."<<endl;
system("pause");
}
file.close();
break;
case 4:
file.open("organization.txt",ios::in);
if(!file)
{
cout<<"Can't open file"<<endl;
system("pause");
exit(0);
}
while(file.read((char*)&o,sizeof(o)))
// loop will iterates until end of read opreation
{ //
which will end at the end of file
cout<<"Name: "<<o.name<<endl;
cout<<"Father Name: "<<o.fathername<<endl;
cout<<"Age: "<<o.age<<endl;
cout<<"Address: "<<o.address<<endl;
cout<<"Worker_no "<<o.worker_no<<endl;
cout<<"Designation: "<<o.Designation<<endl;
cout<<"salary: "<<o.salary<<endl<<endl;
}
file.close();
break;
default:
cout<<"please enter the right number...which are given in
Menu\n\n";
}
cout<<"Go to main menu(y/n)";
cin>>again;
}while(again
== 'y'); //
checking either user want to go back to start of program or not.
} //
ending main
Built in functions used in program relating file
handling:
sizeof(s);
It returns sizeof any object in bytes.Here s is the
object of structre student .
Note:
All function below this described used file handle
with these functions, i.e.
fstream
filehandle;
filehandle.function ( ) ;
open( );
This function is used to open file any mode it uses
2 parameters
Open( “file name and path(not necessay)” , mode) ;
Close( );
This function is used for closing of a file opened
in program every file opened in program must need to close because sometimes if
we don’t close file then I crashes and data written in it will lost.
read( );
this is used to read from file and it uses two
peramenters
read( ( char * ) &s , sizeof ( s ) ) ;
here &s is address of structure and size of
student type object.
write( );
this is used to read from file and it uses two
peramenters
write( ( char * ) &s , sizeof ( s ) ) ;
here &s is address of structure and size of
student type object.
seekp( );
this function is used to move to the required
position in file also called put pointer, sometime it uses two parameter and
sometime one parameter.
seekp( integer ,
ios :: cur ) ;
here integer is size to move from current position.
When one parameter is passed then it moves to the
end of file.
seekp( ios :: end ) ;
Used to move file pointer at end of file.
No comments:
Post a Comment