Skip to main content

File Structure Lab Manual with Algorithms_Program1

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

Popular posts from this blog

Write a program for error detecting code using CRC-CCITT (16- bits).

import java.io.*; import java.util.*; public class CRC   {   public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Enter the no of bits : "); int   n=s.nextInt(); int data[ ]=new int[n]; System.out.println("Enter the data bits : "); for(int i=0;i<n;i++) data[i]=s.nextInt(); System.out.println("Enter the no of divisor bits : "); int m=s.nextInt(); int divisor[ ]=new int[m]; System.out.println("Enter divisor bits : "); for(int j=0;j<m;j++) divisor[j]=s.nextInt(); int len=n+m-1; int div[ ]=new int[len]; int rem[ ]=new int[len]; int crc[ ]=new int[len]; int src[ ]=new int[len]; for(int i=0;i<data.length;i++) div[i]=data[i]; System.out.println("Dividend after appending zero"); for(int i=0;i<div.length;i++) System.out.print(div[i]); System.out.println(); for(int j=0;j<div.length;j++) rem[j]=div[j]; rem=divid

Write a program for congestion control using leaky bucket algorithm

import java.util.*; public class LBA {        public static void main(String[] args ) {              // TODO Auto-generated method stub              int op ;              Scanner s = new Scanner(System. in );              int bktcap =0;              int remain =0;              int pkt []= new int [10];              System. out .println( "Enter Bucket size" );              bktcap = s .nextInt();              System. out .println( "Enter value n" );              int n = s .nextInt();              System. out .println( "Enter datarate" );              int dr = s .nextInt();              System. out .println( "Enter input values" );              for ( int i =0; i < n ; i ++){              int val = s .nextInt();              pkt [ i ]= val ;              }              for ( int i =0; i <= n ; i ++){                     int total = pkt [ i ]+ remain ;                     if ( t