Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

73.Input and output of c language : Formatted text file, fprintf(), fscanf(), feof()

 

Formatted text file:

If the data in a text file is stored in a tabular format that is in rows and columns is called formatted text file.

Every row in a file is called a record which gives the details of an entity (real time object).

fprintf() is used to print the formatted text onto the file.

fscanf() is used to read the formatted text from the file.

feof() is used to find the end of file.

 

fprintf():

It is the function used to write the formatted text onto the file through the file pointer.

It returns the number of bytes written on to the file, returns -1 if any problem in writing on to the file.

 

Function definition:

 

                      int fprintf(FILE *p, char *fs, …..list of variables or fields)

                      {

                            --------------------------------

                            --------------------------------

                            --------------------------------

                      }      

 

Example:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


fscanf():

It is the function used to read the formatted text from the file and store into the specified variables.

It automatically converts the scanned text type into specified variables type.

It returns the number of fields scanned and converted.

 

Function definition:

 

                  int fscanf(FILE *p, char *fs,…list of addresses of variables)

                  {

                         --------------------------------------

                         --------------------------------------

                         --------------------------------------

                  }

Example:

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

feof():

It is the function used to find the end of file.

It accepts the file pointer as argument and returns an integer.

It returns a non-zero (true) if end of file reached otherwise returns 0

 

 Function definition:

                int  feof(FILE *p)

                {

                      -----------------

                      -----------------

                      -----------------

                 }

Specification5:

Accept the details of "n" employees from the keyboard and store into the file "emp"

 

Program:

#include<stdio.h>

void main()

{

   FILE *p;

   int empno,sal;

   char name[20];

   float hra,da,gross;

   int n,i;

   clrscr();

   p=fopen("emp","w");                        /* creating a new file in write mode */

   printf("How many records?");

   scanf("%d",&n);

   printf("Enter %d records:\n",n);

   for(i=1;i<=n;i++)

   {

     printf("\nEmpno:");

     scanf("%d",&empno);

     printf("Name:");

     scanf("%s",name);                           /* reading data from the keyboard */

     printf("Salary:");

     scanf("%d",&sal);

     hra=(float)sal*13/100;

     da=(float)sal*15/100;

     gross=sal+hra+da;

     /* Writing onto the file */     

     fprintf(p,"%d\t%s\t%d\t%f\t%f\t%f\n",empno,name,sal,hra,da,gross);

}

   printf("1 file created with %d records...",n);

   fclose(p);                                            /* closing the file*/    

   getch();

}

 

Execution:

How many records?3

Enter 3 records:

 

Empno:1000

Name:Smith

Salary:2700

 

Empno:1001

Name:Black

Salary:4800

 

Empno:1002

Name:John

Salary:5200

1 file created with 3 records...

 

Program explained:

The file "emp" is opened in write mode using fopen().

Reading the details of employees from the keyboard and writing onto the file using fprintf() through the file pointer

Finally the file is closed using fclose()

 

Spcification6:

Read the data from the file "emp" and display onto the monitor.

 

Program:

#include<stdio.h>

void main()

{

   FILE *p;

   int empno,sal;

   char name[20];

   float hra,da,gross;

   clrscr();

   p=fopen("emp","r");

   printf("Details of employee...\n");

   while(1)

   {

      fscanf(p,"%d%s%d%f%f%f",&empno,name,&sal,&hra,&da,&gross);

      if(feof(p)!=0)

            break;

      printf("Empno:%d\nName:%s\nSalary:%d\n

                      HRA:%f\nDA:%f\nGross:%f\n\n",empno,name,sal,hra,da,gross);

   }

   fclose(p);

   getch();

}

 

Execution:

Details of employee...

Empno:1000

Name:Smith

Salary:2700

HRA:351.000000

DA:405.000000

Gross:3456.000000

 

Empno:1001

Name:Black

Salary:4800

HRA:624.000000

DA:720.000000

Gross:6144.000000

 

Empno:1002

Name:John

Salary:5200

HRA:676.000000

DA:780.000000

Gross:6656.000000

 

Program explained:

The file "emp" is opened in read mode.

Records from the file "emp" have fetched using fscanf() through the file pointer "p".

End of file is checked using feof(p). It returns 0 when end of file is reached.

Records are printed on to the monitor using printf()

Finally the file is closed using feof()

0 comments:

Post a Comment