Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to count number of employee in the company using structures / file

Posted By: Amalasand Miller     Category: C Programming     Views: 4914

Input data regarding the information of the employees of a company. The first field of each input record contains the employee number.Assume that the input data of all the employees is terminated by the trailer record having trailer value of 99999 for the employee number. Write a program using structures/file to count and print total number of input records, that is , the total number of employees.

Code for Program to count number of employee in the company using structures / file in C Programming

#include <stdio.h>
#include <conio.h>
#include "valid.c"struct emp_detail
{  int emp_no;
   char emp_name[10];
   int emp_age;
};

 void getWriteFileName(char *str);
 void getReadFileName(char *str);
 void enterData();
 void getTotalCount();
 void getName(char *);

 FILE *fp;
 int flag=1;
 int count=0;
 char filename[14];
 struct emp_detail emp_cur;

 void main()
 {
    char option;
    clrscr();
    printf("\nDo you want to create the data file type 'y' for yes and 'n' for no.");
    scanf("%c", &option);
    fflush(NULL);

      if( option == 'y' || option == 'Y')
       getWriteFileName(filename);
      elseif( option == 'n' || option == 'N')
       {   getReadFileName(filename);
       getTotalCount();
       }

    getch();
 }

 void enterData()
 {
    printf("\nEnter data .....");
    while(flag)
    {
    //printf("\nEnter employee no. : ");//scanf("%d", &emp_cur.emp_no);
    getPosInteger("Enter employee no. : ", &emp_cur.emp_no);
    fflush(NULL);

    printf("\nEnter employee name : ");
       //scanf("%[^\n]", emp_cur.emp_name);
    getName(emp_cur.emp_name);
    fflush(NULL);

    //printf("\nEnter employee age : ");//scanf("%d", &emp_cur.emp_age);
    getPosInteger("Enter employee age : ", &emp_cur.emp_age);
    fflush(NULL);

    fprintf(fp,"%d %-10s %d", emp_cur.emp_no, emp_cur.emp_name, emp_cur.emp_age);

    if( flag > 0 )
       fprintf(fp,"\n");

    flag++;

    if(emp_cur.emp_no != 9999)
    {   printf("\nEnter 1 to continue and 0 to exit.");
        scanf("%d", &flag);
        fflush(NULL);
    }
    else flag=0;
    count++;
    }

    fclose(fp);
    printf("\nTotal number of employee record entered is : %d", count);
 }

    void getName(char *str)
    {
    int count=0;
    char *getstr;
    char readch='\0';
    int errorflag = 0;

       fflush(NULL);
       getstr = str;

       while(1)
       {
        scanf("%c", &readch);
        if( readch == '\n')
           break;
        if( isalpha(readch) == 0)
        {   printf("\nInvalid entry. Enter characters only.\nEnter name again :");
            errorflag = 1;
            break;
        }
        else
            *getstr = readch;

        count++;
        getstr++;
        if( count >= 10 )
          break;
       }

       if(errorflag)
          getName(emp_cur.emp_name);

      // *getstr='\0';
    }

   void getWriteFileName(char *str)
   {
     printf("Enter Output File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "wt") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
     else
    enterData();
  }

   void getReadFileName(char *str)
   {
     printf("Enter Input File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "r+t") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
   }

    void getTotalCount()
    {  flag =0;
       while( feof(fp) == 0 )
       {
      fscanf(fp, "%d %10s %4d", &emp_cur.emp_name, emp_cur.emp_name, &emp_cur.emp_age);
      flag++;
       }
      fclose(fp);
      printf("\nTotal number of employee record entered is : %d", flag);

    }

    /**************************** Input/Output ********************************/
Do you want to create the data file type 'y'for yes and 'n'for no.y Enter Output File name maxium 14 character : output.txt Enter data ..... Enter employee no. : 1 Enter employee name : chintan Enter employee age : 26 Enter 1 to continue and 0 to exit.1 Enter employee no. : 9999 Enter employee name : nilay Enter employee age : 29 Total number of employee record entered is : 2 /******************************* **********************************/
  
Share: 



Amalasand Miller
Amalasand Miller author of Program to count number of employee in the company using structures / file is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!