Pages

Sunday, 13 June 2021

Programs in turboc3 : Files

File Handling in C
File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our data in Secondary memory (Hard disk). All files related function are available in stdio.h header file.

How to achieve File Handling in C
For achieving file handling in C we need to follow following steps
•Naming a file
•Opening a file
•Reading data from file
•Writing data into file
•Closing a file

Functions use in File Handling in C
S.No Function Operation
1 fopen() To create a file
2 fclose() To close an existing file
3 getc() Read a character from a file
4 putc() Write a character in file
5 fprintf() To write set of data in file
6 fscanf() To read set of data from file.
5 getw() To read an integer from a file
6 putw() To write an integer in file

Defining and Opening a File
Data structure of file is defined as FILE in the standard I/O function. So all files should be declared as type FILE.
Before opening any file we need to specify for which purpose we open file, for example file open for write or read purpose.

Syntax
FILE *fp;
fp=fopen("filename", "mode");
Here fp declare a variable as a pointer to the data type FILE.

Closing a File
A file must be close after completion of all operation related to file. For closing file we need fclose() function.
Syntax
fclose(Filepointer);

File Opening mode
Sno Mode Meaning Purpose
1 r Reading Open the file for reading only.
2 w Writing Open the file for writing only.
3 a Appending Open the file for appending (or adding) data to it.
4 r+ Reading + Writing New data is written at the beginning override existing data.
5 w+ Writing + Reading Override existing data.
6 a+ Reading + Appending To new data is appended at the end of file.

Input/Output Operation on files
To perform Input/Output Operation on files we need below functions.
S.No Function Operation Syntax
1 getc() Read a character from a file getc( fp)
2 putc() Write a character in file putc(c, fp)
3 fprintf() To write set of data in file fprintf(fp, "control string", list)
4 fscanf() To read set of data from file. fscanf(fp, "control string", list)
5 getw() To read an integer from a file. getw(fp)
6 putw() To write an integer in file. putw(integer, fp)


/* Program to read data  from keyboard write it to a file named as INPUT,again read the same data from file and display it on the screen */

#include<stdio.h>
#include<conio.h>
void main()
{
   FILE *f1;
   char c;
   clrscr();
   printf("DATA INPUT\n\n");
   f1 = fopen("INPUT", "w");
   while((c=getchar())!=EOF)
   putc(c,f1);
   fclose(f1);
   printf("DATA OUTPUT\n\n");
   f1 = fopen("INPUT", "r");
   while((c=getc(f1))!=EOF)
   printf("%c",c);
   fclose(f1);
   getch();
}

OUTPUT:
DATA INPUT
this is my first program 
ctrl+z
DATA OUTPUT
this is my first program 


/*  Program to read set of integers into a file name as DATA,and then write all odd numbers to a file named as ODD and all even numbers to a file named as EVEN */ 

#include <stdio.h>
#include <conio.h>
void main()
{
   FILE *f1,*f2,*f3;
   int no,i;
   clrscr();
   printf("CONTENT OF DATA FILE\n\n");
   f1 = fopen("DATA", "w");
   for(i=1;i<=5;i++)
   {
    scanf("%d",&no);
    if(no == -1)
     break;
    putw(no,f1);
   }
   fclose(f1);
   f1=fopen("DATA","r");
   f2=fopen("ODD","w");
   f3=fopen("EVEN","w");
      while((no=getw(f1))!=EOF)
      {
       if(no%2 == 0)
putw(no,f3);
       else
putw(no,f2);
      }
   fclose(f1);
   fclose(f2);
   fclose(f3);
   f2=fopen("ODD","r");
   f3=fopen("EVEN","r");
   printf("\n\nCONTENT OF ODD FILE");
   while((no=getw(f2))!=EOF)
   printf("%4d",no);
   printf("\n\nCONTENT OF EVEN FILE");
   while((no=getw(f3))!=EOF)
   printf("%4d",no);
   fclose(f2);
   fclose(f3);
  getch();
}

output:


/*  Program to read the student data from file named by std and display the file using fprintf and fscanf statements */ 

#include <stdio.h>
void main()
{
   FILE *fp;
   int rno,i,m1,m2,val;
   char name[20],filename[10];
   clrscr();
   printf("INPUT FILE NAME\n\n");
   scanf("%s",filename);
   fp = fopen("filename", "w");
   printf("INPUT THE STUDENT DATA\n");
   printf("name --- rollno  ---  m1 ---- m2   \n");
   for(i=1;i<=3;i++)
   {
    fscanf(stdin,"%s %d %d %d", name,&rno,&m1,&m2);
    fprintf(fp,"%s %d %d %d", name,rno,m1,m2);
   }
   fclose(fp);
   fprintf(stdin,"\n\n");
   fp=fopen("filename","r");
   printf("name --- rollno  ---  m1 ---- m2 --- val\n");
   for(i=1;i<=3;i++)
   {
    fscanf(fp,"%s %d %d %d", name,&rno,&m1,&m2);
    val=m1 + m2;
    printf("\n");
    fprintf(stdout,"%s %d %d %d %d", name,rno,m1,m2,val);
   }
   fclose(fp);
getch();
}

1 comment:

  1. React.js offers a faster development environment, and its very easy to use. Learn them without taking care of too many setups, when you are developing apps by serving HTML files.
    Learn How to Use Reactjs Cdn

    ReplyDelete

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...