Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS151 / KCS251 Programming for Problem Solving - Using C Language


Lab Exercises


38. Write a program to take 10 integers from a file and write the square of these integers in another file.


/*
 File: Prgrm38.c
 Author: Aditya Saini
 Date: Jan 20, 2021
 Description: Program to take 10 integers from a file and write the square of
 these integers in another file.
*/

#include <stdio.h>

int main (void)
{
     FILE *f_ptr_1, *f_ptr_2;
     char path_1[256], path_2[256];
     int num;
     int i;

     //Input first file path
     printf ("Input first file path: ");
     fflush (stdin);
     gets (path_1);

     //Open first file
     f_ptr_1 = fopen (path_1, "r");
     if (f_ptr_1 == NULL)
     {
          printf ("Error! Unable to open first file.");
          return 0;
     }

     //Input second file path
     printf ("Input second file path: ");
     fflush (stdin);
     gets (path_2);

     //Open second file
     f_ptr_2 = fopen (path_2, "w");
     if (f_ptr_2 == NULL)
     {
          printf ("Error! Unable to open second file.");
          return 0;
     }

     //Read integer from first file and write square in second file
     for (i = 0; i <= 9; i++)
     {
          if (fscanf (f_ptr_1, "%d", &num) != EOF)
               fprintf (f_ptr_2, "%d ", num * num);
          else
               break;
     }

     //Close files
     fclose (f_ptr_1);
     fclose (f_ptr_2);

     return 0;
};



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib