Lab # 5

 © Manzur Ashraf

 

Objective:

Learn the Data Files.

 

Scope:

The student should know the following:

  1. Using Data Files for Input
  2. Using Data Files for Output

 

Tips: (Please read out following tips very carefully)

 

FILE *inp, *outp; // Makes cells named inp and outp for storage of file pointers.

 

inp=fopen(“num.dat”, “r”);//It opens num.dat as an input file, storing file pointer in                                              inp.

 

outp= fopen(“num.out”, “w”); // It opens num.out as an output file and stores file                                                   pointer in outp.

 

fscanf(inp, “%d %d”, &mid, &low); // It copies input data from file num.dat into the

                                                         type int variable mid and low.

 

fprintf(outp, “%d %d %d”, high,mid,low) ; // It stores in the file num.out a line                                                                  containing the values of high , mid, and low.

 

fclose(inp); // Closes created input file num.dat

 

fclose(outp); // Closes newly created output file num.out

 

Solved Example :

 

          /* Calculates the area and circumference of a circle given a

            radius value from file c: circle.dat .

            Stores results in c: circle.out

      */

          #include <stdio.h>

          #define PI 3.14159

 

          int main(void)

                   {

                                      double radius, /* input  - radius of a circle   */

                                                           area,   /* output - area of a circle  */

                                                           circum; /* output - circumference of a circle  */

                                      FILE  *inp,    /* pointer to input file   */

                                                          *outp;   /* pointer to output file   */

 

                                      /* Open the input  file   */

                                      inp = fopen("c:circle.dat", "r");

 

                                       /* Open the output file  */

                                      outp = fopen("c:circle.out", "w");

 

                                 /* Read the radius from circle.dat  data file file    */

                                      fscanf(inp, "%lf", &radius);

 

                                /* Stores (Saves ) following fprinf statement’s O/P in circle.out output file */

                                      fprintf(outp, "The radius is %.2f\n", radius);

 

                                      /* Calculate the area    */

                                      area = PI * radius * radius;

 

                                      /* Calculate the circumference   */

                                      circum = 2 * PI * radius;

 

                                      /* Following fprintf statements Stores the area and circumference in    

                                                                  output file circle.out   */

                                      fprintf(outp, "The area is %.2f\n", area);

                                      fprintf(outp, "The circumference is %.2f\n", circum);

 

                                      /* Close input and output files files      */

                                      fclose(inp);

                                      fclose(outp);

 

                                      return (0);

                   } // end of main

 

Excursuses:

 

Problem#1:

 

Compile and Execute program given in solved example and understand it.

Now modify it to store three different values of radius in data file circle.dat and calculate area and circumference for these three different radius of circle present in data file and store these out put in samle.out file.

(Note :Use for loop)

 

Problem#2:

 

Write a program that generates a data file that contains a table for square roots of numbers from 1 to n. The user should enter the value of n from key board.

 

Evaluation:

Your grade will depend on your active participation, individual efforts  and seriousness during the lab.