This post was published long ago, when I was a student and an amateur blogger. The links might be outdated and content may not be useful anymore. Please, read this content keeping its age in mind.

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

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 ]