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

// February 18th, 2009 // 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 ]

Comments

  1. yogesh says:

    hello sir,i am yogesh,ur programs r very good.i want complete programs.and i told to my friends dt u refer these lab programs. thanking u.

  2. yogesh says:

    hello sir,i am yogesh,ur programs r very good.i want complete programs.and i told to my friends dt u refer these lab programs. thanking u.

  3. yogesh says:

    hello sir,i am yogesh,ur programs r very good.i want complete programs.and i told to my friends dt u refer these lab programs. thanking u.

  4. yogesh says:

    how can i get those programs from ur site.plz help me

    1. sandeephegde says:

      Dear Yogesh,

      Thank you for visiting PingSense.com

      I have yet to get the other programs from my madam. Since the lab has been introduced for the first time she too does not have the programs and she is going to write it by her own one by one. I shall post it on the blog as and when I get the programs. I’ll probably be posting the programs every week.

      So keep visiting the site for it :)

      Please tell others about http://www.PingSense.com and ask them to visit the site.

  5. yogesh says:

    how can i get those programs from ur site.plz help me

    1. sandeephegde says:

      Dear Yogesh,

      Thank you for visiting PingSense.com

      I have yet to get the other programs from my madam. Since the lab has been introduced for the first time she too does not have the programs and she is going to write it by her own one by one. I shall post it on the blog as and when I get the programs. I’ll probably be posting the programs every week.

      So keep visiting the site for it :)

      Please tell others about http://www.PingSense.com and ask them to visit the site.

  6. yogesh says:

    how can i get those programs from ur site.plz help me

    1. sandeephegde says:

      Dear Yogesh,

      Thank you for visiting PingSense.com

      I have yet to get the other programs from my madam. Since the lab has been introduced for the first time she too does not have the programs and she is going to write it by her own one by one. I shall post it on the blog as and when I get the programs. I’ll probably be posting the programs every week.

      So keep visiting the site for it :)

      Please tell others about http://www.PingSense.com and ask them to visit the site.

  7. shaardula says:

    hail the lord!some obedient student here :-).plz upload all the pgms of both labs.we barely listen to those sucking,sadist,mentally unstable lecturers but hey that doesn’t mean we don’t want to study!plz help us

    1. sandeephegde says:

      Dear Shaardula,

      Please mind your language. I do not know about your lectures but we do have good lectures in our college. We are proud to have such lectures.

      You might have scored barely sitting the classes till now. If you are that intelligent and can learn everything without the help of a lecture , TRY WRITING FILE STRUCTURE LAB PROGRAMS BY YOUR OWN !

      1. shaardula says:

        Hi! still appreciating your efforts!but just for your info- we got 7 progs from various colleges(even our sources reached upto MS Ramaiah where a workshop on this was held recently),2 from books,3 we friends wrote ourselves(not used your progs).We now have all of them!And we do this all the time! But still there are lot many benefitting from you work keep it going.

        1. sandeephegde says:

          Dear Shaardula,

          Thank you for your compliments.

          I congratulate for your efforts. Meanwhile I’m just uploading the programs so that it will be of use to someone and its not mandatory for them to use it if they have an alternative.

          Currently I’m in a holiday mood since our college is taken over for election duty(17-4-2009 to 17-5-2009). I could not even update the site since some time as I did not have access to internet at home.

          Will try to put up the other programs meanwhile even you can share if you are interested :)

  8. shaardula says:

    hail the lord!some obedient student here :-).plz upload all the pgms of both labs.we barely listen to those sucking,sadist,mentally unstable lecturers but hey that doesn’t mean we don’t want to study!plz help us

    1. sandeephegde says:

      Dear Shaardula,

      Please mind your language. I do not know about your lectures but we do have good lectures in our college. We are proud to have such lectures.

      You might have scored barely sitting the classes till now. If you are that intelligent and can learn everything without the help of a lecture , TRY WRITING FILE STRUCTURE LAB PROGRAMS BY YOUR OWN !

      1. shaardula says:

        Hi! still appreciating your efforts!but just for your info- we got 7 progs from various colleges(even our sources reached upto MS Ramaiah where a workshop on this was held recently),2 from books,3 we friends wrote ourselves(not used your progs).We now have all of them!And we do this all the time! But still there are lot many benefitting from you work keep it going.

        1. sandeephegde says:

          Dear Shaardula,

          Thank you for your compliments.

          I congratulate for your efforts. Meanwhile I’m just uploading the programs so that it will be of use to someone and its not mandatory for them to use it if they have an alternative.

          Currently I’m in a holiday mood since our college is taken over for election duty(17-4-2009 to 17-5-2009). I could not even update the site since some time as I did not have access to internet at home.

          Will try to put up the other programs meanwhile even you can share if you are interested :)

  9. shaardula says:

    hail the lord!some obedient student here :-).plz upload all the pgms of both labs.we barely listen to those sucking,sadist,mentally unstable lecturers but hey that doesn’t mean we don’t want to study!plz help us

    1. sandeephegde says:

      Dear Shaardula,

      Please mind your language. I do not know about your lectures but we do have good lectures in our college. We are proud to have such lectures.

      You might have scored barely sitting the classes till now. If you are that intelligent and can learn everything without the help of a lecture , TRY WRITING FILE STRUCTURE LAB PROGRAMS BY YOUR OWN !

      1. shaardula says:

        Hi! still appreciating your efforts!but just for your info- we got 7 progs from various colleges(even our sources reached upto MS Ramaiah where a workshop on this was held recently),2 from books,3 we friends wrote ourselves(not used your progs).We now have all of them!And we do this all the time! But still there are lot many benefitting from you work keep it going.

        1. sandeephegde says:

          Dear Shaardula,

          Thank you for your compliments.

          I congratulate for your efforts. Meanwhile I’m just uploading the programs so that it will be of use to someone and its not mandatory for them to use it if they have an alternative.

          Currently I’m in a holiday mood since our college is taken over for election duty(17-4-2009 to 17-5-2009). I could not even update the site since some time as I did not have access to internet at home.

          Will try to put up the other programs meanwhile even you can share if you are interested :)

  10. praveen says:

    Thanks a lot for FS lab program… Ill be waiting for other programs.. hope it comes soon in this site…:-) Im grate full to you n to ur madam……:-) :-)

    1. sandeephegde says:

      Dear Praveen,
      Thank you for your comments.

      The File structure programs are very tough. Even we have executed only one program in lab till now and yet to get the remaining programs.

      Mini Project was far better than this lab :)

      God knows how we are going to clear this lab :(

      1. prithu.a.roy says:

        hi sandy thanks for showing concern about this topic well our college is going pretty fast on this topics we have already executed 4 programmes in lab i would like to shair source code of second programme i am sending it in it just add it to your web page veify it before adding and will will send u rest of the programmes after u add it once agan thaks for concern

        second programme:-
        http://www.pingsense.com/2009/03/file-structure-fs-lab-program-2-6th-semester-information-science-2/

        1. sandeephegde says:

          Dear Prithu,

          Thnx for your concern too.

          If everyone share resources like you it, engineering gets easier :)

          I have posted your program at : http://www.pingsense.com/2009/03/file-structure-fs-lab-program-2-6th-semester-information-science-2/

          1. prithu.a.roy says:

            Thanks
            If that’s your motive then i will be ready with what ever i could with the best of i can…….
            well as far as the third program is concerned its same as second oneone 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

  11. praveen says:

    Thanks a lot for FS lab program… Ill be waiting for other programs.. hope it comes soon in this site…:-) Im grate full to you n to ur madam……:-) :-)

    1. sandeephegde says:

      Dear Praveen,
      Thank you for your comments.

      The File structure programs are very tough. Even we have executed only one program in lab till now and yet to get the remaining programs.

      Mini Project was far better than this lab :)

      God knows how we are going to clear this lab :(

      1. prithu.a.roy says:

        hi sandy thanks for showing concern about this topic well our college is going pretty fast on this topics we have already executed 4 programmes in lab i would like to shair source code of second programme i am sending it in it just add it to your web page veify it before adding and will will send u rest of the programmes after u add it once agan thaks for concern

        second programme:-
        http://www.pingsense.com/2009/03/file-structure-fs-lab-program-2-6th-semester-information-science-2/

        1. sandeephegde says:

          Dear Prithu,

          Thnx for your concern too.

          If everyone share resources like you it, engineering gets easier :)

          I have posted your program at : http://www.pingsense.com/2009/03/file-structure-fs-lab-program-2-6th-semester-information-science-2/

          1. prithu.a.roy says:

            Thanks
            If that’s your motive then i will be ready with what ever i could with the best of i can…….
            well as far as the third program is concerned its same as second oneone 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

  12. praveen says:

    Thanks a lot for FS lab program… Ill be waiting for other programs.. hope it comes soon in this site…:-) Im grate full to you n to ur madam……:-) :-)

    1. sandeephegde says:

      Dear Praveen,
      Thank you for your comments.

      The File structure programs are very tough. Even we have executed only one program in lab till now and yet to get the remaining programs.

      Mini Project was far better than this lab :)

      God knows how we are going to clear this lab :(

      1. prithu.a.roy says:

        hi sandy thanks for showing concern about this topic well our college is going pretty fast on this topics we have already executed 4 programmes in lab i would like to shair source code of second programme i am sending it in it just add it to your web page veify it before adding and will will send u rest of the programmes after u add it once agan thaks for concern

        second programme:-
        http://www.pingsense.com/2009/03/file-structure-fs-lab-program-2-6th-semester-information-science-2/

        1. sandeephegde says:

          Dear Prithu,

          Thnx for your concern too.

          If everyone share resources like you it, engineering gets easier :)

          I have posted your program at : http://www.pingsense.com/2009/03/file-structure-fs-lab-program-2-6th-semester-information-science-2/

          1. prithu.a.roy says:

            Thanks
            If that’s your motive then i will be ready with what ever i could with the best of i can…….
            well as far as the third program is concerned its same as second oneone 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

  13. prithu.a.roy says:

    hi can u represent a bit ans give the third program i have executed it its third program
    representation means by showing output and objectives of the program
    u can take the credits too because its not actully me writing the prog i am just a mediator to summit…….

    3rd Program :
    http://www.pingsense.com/2009/03/file-structure-fs-lab-program-3-6th-semester-information-science/

    1. sandeephegde says:

      Dear Prithu,

      Thanx for sharing the programs. Although you think you are just a mediator, I believe you own a credit of sharing the programs :)

      I have posted your 3rd Program at : http://www.pingsense.com/2009/03/file-structure-fs-lab-program-3-6th-semester-information-science/

  14. prithu.a.roy says:

    hi can u represent a bit ans give the third program i have executed it its third program
    representation means by showing output and objectives of the program
    u can take the credits too because its not actully me writing the prog i am just a mediator to summit…….

    3rd Program :
    http://www.pingsense.com/2009/03/file-structure-fs-lab-program-3-6th-semester-information-science/

    1. sandeephegde says:

      Dear Prithu,

      Thanx for sharing the programs. Although you think you are just a mediator, I believe you own a credit of sharing the programs :)

      I have posted your 3rd Program at : http://www.pingsense.com/2009/03/file-structure-fs-lab-program-3-6th-semester-information-science/

  15. prithu.a.roy says:

    hi can u represent a bit ans give the third program i have executed it its third program
    representation means by showing output and objectives of the program
    u can take the credits too because its not actully me writing the prog i am just a mediator to summit…….

    3rd Program :
    http://www.pingsense.com/2009/03/file-structure-fs-lab-program-3-6th-semester-information-science/

    1. sandeephegde says:

      Dear Prithu,

      Thanx for sharing the programs. Although you think you are just a mediator, I believe you own a credit of sharing the programs :)

      I have posted your 3rd Program at : http://www.pingsense.com/2009/03/file-structure-fs-lab-program-3-6th-semester-information-science/

  16. prithu.a.roy says:

    /*c++ programme to read and write students object with variable length records and
    fields delimeted by’|’Implemend pack(),UNPACK(),modify()and search() methods*/
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    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);
    }
    }
    }

  17. samarth says:

    hey thanks a lot for the programs dude

  18. Shanu says:

    thx….

  19. Rekhareddyv says:

    pls give explanation for all the programs