ICS 103: Computer Programming in C

Handout-08

Topic: Data Files

 

Instructor: M. Waheed Aslam.

 

Objective:

What is the Need of Data File?

·        Two basic mode of entering data into the program:

1.  Interactive mode:

§        User interacts with the program and types in data while it is running

§        Prompts are included to tell user when to enter each data item

2.  Batch mode:

§        The program scans its data from a data file prepared in advanced.

§        No interaction or little interaction with the user.

·        Data files are of two main types:

1.  input data file

2.  output data file.

·        Input data is permanently stored in input data file and you can use this data file any time later.

·        For input file:

§        First, create input data file on some secondary storage device

§        Then, input data and save it in the input file permanently instead of simply taking data temporarily from keyboard. Doing this, the input data will be permanently stored in this input data file and we can simply use this file any time.

 

·        Similarly we can save our outputs in Output data file permanently in place of simply displaying outputs only on the screen of the computer.

 

Important Statements/Functions for handling I/O Data files:

List of Some useful Functions/Statements and their purpose is explained below:

  

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.  If this file is not opened properly then it returns NULL value.

 

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

          // pointer in outp.  If this file is not opened properly then it returns NULL value.

 

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

 

Example 1:

          /* Calculates the area and circumference of a circle by taking */

      /*    radius value from file input file  circle.txt and  Stores */           

      /*  results/output  in circle.out outputfile. */

          #include <stdio.h>

      #include <stdlib.h> // required for exit function prototype

      #define PI 3.14159

 

      int main(void) //As you know  here void with in brackets is optional

                {

                    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("circle.txt", "r");

          

/* Before using above statement circle.txt input file must be created by you

/* and after creation you must save some radius in this input file */

if(inp==NULL)//to check input file is opened properly or not

               {

                  printf("Not able to open input file");

                  exit(1); // terminates the program

               } // end of if

 

                            /* Open the output file */

                    outp = fopen("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   */

                                fclose(inp);

                        fclose(outp);

 

                                return (0); // returns 0 integer value to operating system

                } // end of main

 

Sample Output:

The radius is 5.00

The area is 78.54

The circumference is 31.42

 

Example 2:

/*Miles-to-Kilometers Conversion Program with Named Files */

/* Converts distances from miles to kilometers.  */

 

#include <stdio.h>    /* printf, scanf, fprint, fscanf, fopen, fclose  definitions */

#include<stdlib.h> // it used here because of exit( ) function

#define KMS_PER_MILE 1.609 /* conversion constant       */

int main(void)

{

       double miles,  /* distance in miles */

                  kms;   /* equivalent distance in kilometers */

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

              *outp;   /* pointer to output file */

 

      /* Open the input and output files. */

                inp = fopen("distance.txt", "r");

                outp = fopen("distance.out", "w");

 

                if(inp==NULL) // for testing input file is opened properly or not

                {

                printf("Unable to open input file");

                exit(1);

                } // end of if

 

                /* Get and echo the distance in miles. */

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

      fprintf(outp, "The distance in miles is %.2f.\n", miles);

 

      /* Convert the distance to kilometers.   */

      kms = KMS_PER_MILE * miles;

 

      /* Display the distance in kilometers.     */

      fprintf(outp, "That equals %.2f kilometers.\n", kms);

 

    /* Close all opened  files. */

      fclose(inp);

      fclose(outp);

 

      return (0); // returns integer value 0 to operating system

} // end of main

 

/***********************************/

For testing use following data:

 

Contents of input file distance.txt:

112.0

 

Contents of output file distance.out will be:

 

The distance in miles is 112.00.

That equals 180.21 kilometers.  

*************************************/