Posts Tagged ‘6th semester file structure lab’

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

// April 5th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<conio.h>

#include<string.h>

class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};

void search()
{
char usn[15];
int i=0;
student s1[100];
cout<<”n Enter the usn to be searched”;
cin>>usn;
fstream in;
in.open(“student.txt”,ios::in);
if(!in)
{
cout<<”n Cannot open the file in output mode”;
getch();
exit(0);
}

i=0;
cout<<”NametUsntAgetSemtBranch”;
while(!in.eof())
{
in.getline(s1[i].name,15,’|');
in.getline(s1[i].usn,15,’|');
in.getline(s1[i].age,5,’|');
in.getline(s1[i].sem,5,’|');
in.getline(s1[i].branch,15,’n');
i++;
}

for(int j=0;j<i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<”Record Foundn”;
cout<<”n”<<s1[j].name<<”t”<<s1[j].usn<<”t”<<s1[j].age<<”t”<<s1[j].sem<<”t”<<s1[j].branch;
return;
}
}
cout<<”n Record not found”;
return;
}

void writerecord()
{
fstream app;
student s;
app.open(“student.txt”,ios::app);
if(!app)
{
cout<<” Cannot open the file in output mode”;
getch();
exit(0);
}

cout<<”nenter the student name =”;
cin>>s.name;
cout<<”nEnter the usn =”;
cin>>s.usn;
cout<<”n Enter the age = “;
cin>>s.age;
cout<<”n Enter the sem =” ;
cin>>s.sem;
cout<<”n Enter the branch =”;
cin>>s.branch;

strcpy(s.buffer,s.name);
strcat(s.buffer,”|”);
strcat(s.buffer,s.usn);
strcat(s.buffer,”|”);
strcat(s.buffer,s.age);
strcat(s.buffer,”|”);
strcat(s.buffer,s.sem);
strcat(s.buffer,”|”);
strcat(s.buffer,s.branch);
strcat(s.buffer,”n”);

app<<s.buffer;

app.close();
}

void main()
{
clrscr();
int ch;
fstream out;
out.open(“student.txt”,ios::out);
if(!out)
{
cout<<”Cannot open the file in output mode”;
getch();
exit(0);
}

out.close();
for(;;)
{
cout<<”n1. Insert”;
cout<<”n2. Search”;
cout<<”n3. Exit”;

cout<<”Enter the choice : “;
cin>>ch;

switch(ch)
{
case 1: writerecord(); break;
case 2: search(); break;
case 3: exit(0);
default: cout<< “n Invalid Choice”;

}
}
}

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

// March 31st, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

/*c++ program to read and write students object with variable length records and
fields delimited 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,’#’);
count++;
if(atoi(usn)==atoi(reg))
{
cout<<”nrecord found”<<count<<”positionn”;
readrec();
pack();
write(file1);
cout<<”nrecord modified.n”;
return;
}
else if(strlen(name)==0)
break;
pack();
write(file1);
}
cout<<”nrecord not foundn”;
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,’#’);
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,’#’);
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,”#”);
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 recordn”;
cout<<”3:modify recordt4:search recordn”;
cout<<”press 5 to exitn”;
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

File Structure ( FS ) Lab Program 2 ( Set 2 )

// March 30th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

/*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<process.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
class Student
{
public:
char buf[40],name[10],semes[10],branch[10];
void unpack(ifstream & ifile);
void pack(ofstream & ofile);
void search(ifstream & ifile, char key[]);
void modify(fstream & iofile, char key[]);
};

void Student::pack(ofstream & ofile)
{
clrscr();
strcpy(buf,”");
cout<<”Name “<<endl;
cin>>name;
strcat(buf,name);
strcat(buf,”|”);
cout<<”Semester “<<endl;
cin>>semes;
strcat(buf,semes);
strcat(buf,”|”);
cout<<”Branch “<<endl;
cin>>branch;
strcat(buf,branch);
strcat(buf,”|”);
while(strlen(buf)<40)
{
strcat(buf,”.”);
}
strcat(buf,”n”);
ofile.write(buf,strlen(buf));
}
void Student::unpack(ifstream & ifile)
{
char *field;
clrscr();
while(1)
{
strcpy(buf,”");
ifile.getline(buf,42,’n');
if(ifile.fail())
break;
int  p=0,q;
for (q = 0;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;
cout<<”Name: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;
cout<<”Semester: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;
cout<<”Branch: “<<field<<endl<<endl;
}
}
void Student::search(ifstream & ifile, char key[])
{
char field[10];
int flag = 0;
clrscr();
while(1)
{
strcpy(buf,”");
ifile.getline(buf,42,’n');
if(ifile.fail())
break;
int  p=0,q;
for (q = 0;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;

if(strcmp(field,key)==0)
{
cout<<”Record found and details are:”<<endl;
cout<<”Name: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;
cout<<”Semester: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;
cout<<”Branch: “<<field<<endl;
flag=1;
break;
}
}
if(!flag)
cout<<”Record with given key not found”<<endl;
}
void Student::modify(fstream & iofile, char key[])
{
char field[10];
//     int flag = 0;
clrscr();
while(1)
{
strcpy(buf,”");
iofile.getline(buf,42,’n');
if(iofile.fail())
break;
int  p=0,q;
for (q = 0;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘’;
if(strcmp(field,key)==0)
{
clrscr();
cout<<”Record found Enter modification details”<<endl;
iofile.seekp(-42,ios::cur);
//pack(iofile);
strcpy(buf,”");
cout<<”Name “<<endl;
cin>>name;
strcat(buf,name);
strcat(buf,”|”);
cout<<”Semester “<<endl;
cin>>semes;
strcat(buf,semes);
strcat(buf,”|”);
cout<<”Branch “<<endl;
cin>>branch;
strcat(buf,branch);
strcat(buf,”|”);
while(strlen(buf)<40)
{
strcat(buf,”.”);
}
strcat(buf,”n”);
iofile.write(buf,strlen(buf));
break;
}
}
}

void main()
{
int n,i=0,ch;
Student stu;
clrscr();
for(;;)
{
clrscr();
cout<<”1. Insertn2.  Display alln3.  Searchn4.  Modifyn5.  Exitn”;
cout<<”Enter your choice”<<endl;
cin>>ch;
switch(ch)
{
case 1:    ofstream ofile;
ofile.open(“student.txt”,ios::out|ios::trunc);
cout<<”enter the no. of students”<<endl;
cin>>n;
while(i<n)
{
stu.pack(ofile);
i++;
}
ofile.close();
break;

case 2:    ifstream infile;
infile.open(“student.txt”,ios::in);
stu.unpack(infile);
getch();
infile.close();
break;

case 3: cout<<”Enter the record name to be searched”<<endl;
char key[10];
cin>>key;
ifstream ifile;
ifile.open(“student.txt”,ios::in);
stu.search(ifile,key);
getch();
ifile.close();
break;

case 4:    fstream iofile;
iofile.open(“student.txt”,ios::in|ios::out);
cout<<”Enter the record name to be modified”<<endl;
cin>>key;
stu.modify(iofile,key);
getch();
iofile.close();
break;

default: exit(0);
}
}
}

Credits : Sahana V

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

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

/*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<<”positionn”;
readrec();
pack();
write(file1);
cout<<”nrecord modified.n”;
return;
}
else if(strlen(name)==0)
break;
pack();
write(file1);
}
cout<<”nrecord not foundn”;
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 recordn”;
cout<<”3:modify recordt4:search recordn”;
cout<<”press 5 to exitn”;
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

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

// March 25th, 2009 // Comments // Educational, Engineering, File Structure (F.S.) Lab

FILE STRUCTURES LAB

Semester : 6

Subject Code : O6ISL67

Experiment No.  :  2


Objective   of the Experiment :
To make objects persistet by writing them to file . To maintain identity of fields and records , add delimiters to fields  and make  records  of fixed length.
Build two separate classes Student and FixedLengthBuffer (Record size =75). Write  overloaded insertion and extraction operators for reading and displaying  student data  . Implement  Pack() , UnPack() ,Read(), Write () methods for performing Buffer and file  opeartions.Use ‘ | ‘ as field Delimiter.
Class should have constructor to initialize buffer and student objects.

Procedure  in main :
1.    Create Student object and initialize
2.    Create buffer object and initialize
3.    Open file in write mode
4.    Read data for student
5.    Pack the student object into buffer
6.    Write the buffer to file
7.    Repeat step 4-6 for 15 students
8.    Close  the file.
9.    Open the file in read mode
10.    Read a record into buffer
11.    Unpack the fields and restore the state of an Object
12.    Display student record
13.    Repeat steps 10-12 for all the records stored in the file
14.    Close the file.


Buffer Contents                                               Record  USN  Name              Address            Semester
11 | student1 |Belgaum |1…………….        1                11         student1      Belgaum          1
12 |student2 |bangalore |2……………    2               12         student2     bangalore       2

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

// February 18th, 2009 // Comments // Engineering, File Structure (F.S.) Lab

[Click Here to get remaining programs by email ]

…………………………………………………………………………………………

Subject : File Structures lab

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………..


Experiment No. : 1

Objective of the program : To learn basic file processing operations and to use IO redirection and pipes.

Problem statement : Write a c++ program to read series of names one per line, from standard input and write these names spelled in reverse order to the standard output. Use IO redirection and pipes. Repeat the exercise using an input file specified by the user instead of the standard input and using an output file specified by the user instead of the standard output.

……………………………………………………………………………………………

#include<iostream.h>

#include<string.h>

#include<fstream.h>

int main()

{

char s1[25];

fstream file1,file2;

int i=0,j=0,x=0,c=0,kb=0;

char filename1[25],filename2[25];

cout<<endl<<”1 for standard I/O : “<<endl<<”2 for file I/O : “<<endl<<”Enter your choice : “;

cin>>kb;

switch(kb)

{

case 1 :

cout<< Enter Name Count : “;

cin>>c;

for(i=1;j<=c;j++)

{

cout<<endl<<”Enter Name “<<j<<” : “;

cin>>s1;

x=strlen(s1);

cout<<” Reserved name “<<j<<” : “;

//Reverse name

for(i=x-1;i>=0;i-)

cout<<s1[i];

cout<<endl;

}

break;

case 2 :

cout<<”Enter Data file name ;”;

ci>>filename1;

cout<<Enter Reverse data filename : “;

cin>>filename2;

file1.open(filename1,jos::in);

file2.open(filename2,jos::out);

while(1)

{

file1.getline(s1,25);

if(file1.fail())

break;

x=strlen(s1);

//Reverse name and send to file

for(i=x-1;i>=0;i–)

file2<<s1[i];

file2<<endl;

}

file1.close();

file2.close();

break;

}

returen 1;

}

___________________________________________


Sample Input / Output

___________________________________________

Using Standard I/O

1 for standard I/O

2 for file I/O :

Enter your choice : 1

Enter name count : 3

Enter Name 1 : GIT

Reversed name 1 : TIG

Enter Name 2 : Information

Reversed name 2 : noitamrofnI

Enetr Name 3 : Science

Reversed name 3 : ecneicS

……………………………………………………………….


Using File I/O

1 for standard I/O

2 for file I/O :

Enter your choice : 2

Enter Data file name : names.txt

Enter Reverse data filename : rev.txt

[root@localhost~]# cat names.txt

KLS

GOGTE

INFORMATION

SCIENCE

[root@localhost~]# cat rev.txt

SLK

ETGOG

NOITAMROFNI

ECNEICS

…………………………………………………………………….

IO redirection and pipes

Pipe

root@localhost~]# cat rev.txt | sort

ECNEICS

ETGOG

NOITAMROFNI

SLK

Redirection of standard output :

[root@localhost~]# cat rev.txt | sort>sortedRev.txt

[root@localhost~]# cat sortedRev.txt

ECNEICS

ETGOG

NOITAMROFNI

SLK

Click Here for 2nd Program

[Click Here to get remaining programs by email ]

[Request : If you are going to post this program on any other website please link back to original post ]