Program 1. Write a program to read series of names, one per line, from standard input and write these names spelled in reverse order to the standard output using I/O 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.
_Algorithm:
Step 1: Start
Step 2: Enter your choice 1. From standard I/O 2. From Files
Step 3: Select the choice- 1: Using Standard file 2: Using Files
Case 1: [using standard I/O]
Enter the number of names to be read
Read the value of N
a. Read one name at a time
b. call reverse(name) function
display the reversed name on standard output
Repeat a and b for N times
Case 2: [Using Files]
Open the file for reading
Read names from an input file
Reverse the name by calling strrev(name) and redirect results to a file.
Step 4: Close the file
Step 5: Stop
Program:
// Program to read the series of names, one per line, from input and write these names spelled in
reverse order to the output using I/O redirection and pipes
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
class std_file
{
private:
char name[10][20];
char input[20], output[20],str[20];
public:
void std_io();
void file_io();
};
void std_file::std_io()
{
intn,i;
cout<<"Enter the number of names to read "<<endl;
cin>>n;
cout<<"Enter the names"<<endl;
for(i=0;i<n;i++)
gets(name[i]);
cout<<"The reversed names are"<<endl;
for(i=0;i<n;i++)
cout<<strrev(name[i])<<endl;
}
void std_file::file_io()
{
fstreamifile,ofile;
cout<<"Enter the filename which contain list of names"<<endl;
cin>>input;
ifile.open(input,ios::in);
if(!ifile)
{
cout<<"File does not exist";
exit(0);
getch();
}
cout<<"Enter the filename to store names in reverse order"<<endl;
cin>>output;
ofile.open(output,ios::out);
while(!ifile.eof())
{
ifile.getline(str,20);
ofile<<strrev(str)<<endl; //to reverse string characters
}
}
void main()
{
int num;
std_file s;
clrscr();
for(;;)
{
cout<<"1: Using standard I/O 2: Using files”<< endl;
cout<<"Enter the choice”;
cin>>num;
switch(num)
{
case 1: s.std_io();
break;
case 2: s.file_io();
break;
default: exit(0);
}
}
}
Output 1:
1: Using standard I/O 2: Using files
Enter the choice 1
Enter the number of names to read 3
Enter the names
akash
rama
Deepak
The reversed names are
hsaka
amar
kapeed
Viva Voce:
1. What is file structure?
File structure is a combination of representations for data in files and of operations for
accessing the data.
2. Define File.
A file is a container in a computer system for storing information.
3. List different types of file.
Types of files are: Physical files and Logical files.
4. What is datatype?
It is an attribute of data which tells the compiler or interpreter how the programmer intends to
use the data.
5. List different types of datatypes.
Integer, Floating-point number, Character, String, Boolean.
6. Difference between data structure and file structure.
The representation of the particular data structure in the memory of a computer is called a
storage structure whereas a storage structure representation in auxiliary memory is often
called a file structure.
7. List basic file input operations used.
Writing into the file
8. List basic file output operations used.
Reading from the file
9. Basic file operations and their syntactic structures in C and C++
In C: fopen(fname, ftype), fread(void *buf, size_t size, size_t num, FILE *fp)
In C++: Using constructor: ifstream fin; fin.open(“filename”);
Comments
Post a Comment