Enter your email address & Win:

File Structure ( FS ) Lab Program 2 ( 6th Semester Information Science)

// March 29th, 2009 // Educational, Engineering, File Structure (F.S.) Lab // Written by

/*c++ programme to read and write students object with fixed length records and
fields delimeted by’|’Implemend pack(),UNPACK(),modify()and search() methods*/
#include iostream.h
#include conio.h
#include fstream.h
#include string.h
#include process.h
#include stdlib.h
#include stdio.h

class student
{
public:
char usn[100],name[100],address[100],branch[100],college[100],buffer[100];
char sem[100];
void readrec();
void pack();
void write(fstream&file);
void unpack(fstream&file);
void search(fstream&file);
void modify(fstream&file);
};
void student::modify(fstream&file)
{
char reg[80];
int count=0;
file.seekg(0);
fstream file1;
file1.open(”data2.txt”,ios::out);
cout<>reg;
while(1)
{
file.getline(usn,80,’|’);
file.getline(name,80,’|’);
file.getline(address,80,’|’);
file.getline(sem,80,’|’);
file.getline(branch,80,’|’);
file.getline(college,80,’\n’);
count++;
if(atoi(usn)==atoi(reg))
{
cout<<”\nrecord found”<<count<<”position\n”;
readrec();
pack();
write(file1);
cout<<”\nrecord modified.\n”;
return;
}
else if(strlen(name)==0)
break;
pack();
write(file1);
}
cout<<”\nrecord not found\n”;
file1.close();
}

void student::search(fstream&file)
{
char reg[80],temp[80];
int count=0;
file.seekg(0);
cout<>reg;
while(1)
{
file.getline(usn,80,’|’);
strcpy(temp,usn);
file.getline(name,80,’|’);
file.getline(address,80,’|’);
file.getline(sem,80,’|’);
file.getline(branch,80,’|’);
file.getline(college,80,’\n’);
count++;
if(atoi(temp)==atoi(reg))
{
cout<<”\nRecord found at”<<count<<”position”;
return;
}
if(strlen(usn)==0)
break;
}
cout<<”\nRecord not found”;
}
void student::unpack(fstream&file)
{
while(1)
{
file.getline(usn,80,’|’);
file.getline(name,80,’|’);
file.getline(address,80,’|’);
file.getline(sem,80,’|’);
file.getline(branch,80,’|’);
file.getline(college,80,’\n’);
if(strlen(usn)==0)
break;
cout<<flush<<”\nusn:”<<usn<<endl;
cout<<”\nname:”<<name<<endl
<<”\naddress:”<<address<<endl
<<”\nsemister:”<<sem<<endl
<<”\nBranch:”<<branch<<endl
<<”\ncollege:”<<college<<endl;
cout<<”\n**************\n”;
}
}
void student::pack()
{
char tempbuf[100];
tempbuf[0]=”;
strcat(tempbuf,usn);
strcat(tempbuf,”|”);
strcat(tempbuf,name);
strcat(tempbuf,”|”);
strcat(tempbuf,address);
strcat(tempbuf,”|”);
strcat(tempbuf,sem);
strcat(tempbuf,”|”);
strcat(tempbuf,branch);
strcat(tempbuf,”|”);
strcat(tempbuf,college);
strcat(tempbuf,”\n”);
strcpy(buffer,tempbuf);
}
void student::write(fstream&file)
{
file<<buffer;
}
void student::readrec()
{
cout<>usn;
cout<>name;
cout<>address;
cout<>sem;
cout<>branch;
cout<>college;
}
void main()
{
char filename[25];
int choice;
fstream file;
student st;
clrscr();
cout<>filename;
while(1)
{
cout<<”\n*******program|menue********\n”;
cout<<”1:pack Record \t2:unpack record\n”;
cout<<”3:modify record\t4:search record\n”;
cout<<”press 5 to exit\n”;
cout<<”****************************\n”;
cout<>choice;
switch(choice)
{
case 1:st.readrec();
st.pack();
file.open(filename,ios::out|ios::app|ios::in);st.write(file);
file.close();
break;
case 2:file.open(filename,ios::out|ios::app|ios::in);
st.unpack(file);
file.close();
break;
case 3:file.open(filename,ios::out|ios::app|ios::in);
st.modify(file);
file.close();
unlink(filename);
rename(”data2.txt”,filename);
break;
case 4:file.open(filename,ios::out|ios::app|ios::in);
st.search(file);
file.close();
break;
case 5:exit(0);
}
}
}

Credits : Prithu  A Roy

If you enjoyed this post, make sure you subscribe to my RSS feed!

  • Pingback: PingSense.com :: File Structure ( FS ) Lab Program :: 6th Semester Information Science | PingSense

    • http://no prithu.a.roy

      hi all
      the third program is literally same as the second one only difference is in second program we are storing each record of student in one line hence length of line is fixed and program determines it by \n ie newline

      so for the third program we have variable length record hence the record should be differentiated from each other by some diff delimiter i have taken #

      in short we have to replace \n by # in the second programme

  • prithu.a.roy

    hi all
    the third program is literally same as the second one only difference is in second program we are storing each record of student in one line hence length of line is fixed and program determines it by n ie newline

    so for the third program we have variable length record hence the record should be differentiated from each other by some diff delimiter i have taken #

    in short we have to replace n by # in the second programme

  • prithu.a.roy

    hi all
    the third program is literally same as the second one only difference is in second program we are storing each record of student in one line hence length of line is fixed and program determines it by n ie newline

    so for the third program we have variable length record hence the record should be differentiated from each other by some diff delimiter i have taken #

    in short we have to replace n by # in the second programme

  • prade23

    hiiii…..i needed all the FS programs urgently

  • Siddivinayak676

    hello sir,
             
                can these programs be executed using fedora
     

Get Adobe Flash playerPlugin by wpburn.com wordpress themes