Showing posts with label Make. Show all posts
Showing posts with label Make. Show all posts

C Program To Make Employee Payment Record Using Structure

How to write a C Program To Make Employee Payment Record Using Structure in C Programming Language ?


Solution For C Program :

/*C Program To Make Employee Payment Record Using Structure.*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
    {
    int bs,att;
    char name[10];
    };
struct employee emp[10];
void main()
{
int i;
float ded,da,hra,net,gross,sal;
clrscr();
for(i=1;i<4;i++)
    {
    printf("enter the name of %d employee: ",i);
    scanf("%s",emp[i].name);
    printf("enter the attendence of preset month = ");
    scanf("%d",&emp[i].att);
    printf("enter the basic salary =");
    scanf("%d",&emp[i].bs);
    }

printf("\n\ndata stored are :\n");
for(i=1;i<4;i++)
    {
    printf("Name of %d employee: %s\n",i,emp[i].name);
    sal=(emp[i].bs/30)*emp[i].att;
    da=sal*0.1;
    hra=sal*0.2;
    gross=sal+da+hra;
    ded=gross*0.1;
    net=gross-ded;
    printf("Gross salary =%f\n",gross);
    printf("Net salary =%f\n",net);
    }
getch();
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

Make a copy of file given in argv[1] to file given in argv[2]

How to write a C Program to Make a copy of file given in argv[1] to file given in argv[2] in C Programming Language ?

You have to use program with two arguments, the file names copy_from copy_to.

This C Program Make a copy of file given in argv[1] to file given in argv[2].

There is an access function with which we can see whether the file exists or not

 access(filename,F_OK);
 open 1. parameter= file name
 open 2. parameter for what we want to use the file
 O_RDONLY=only for reading,O_WRONLY-only for writing

There is errno variable in which there is the number of error --

 if (errno!=0) there is an error

The three parameter long version of open - it can create the file if it doesnt exist

there is a creat function as well - creat(filename,permissions);
O_TRUNC = if it existed, clear the content,
O_CREAT=create if it doesn't exist
3. parameter the permission, if it has to be created
S_IRUSR=permission for reading by the owner e.g.

Read gives back the number of bytes

    1. parameter the file descriptor
    2. parameter the address of variable, we read into
    3. parameter the number of bytes we want to read in

Write gives back the number of written bytes

      1. parameter the file descriptor
      2. parameter the address of variable we want to write out
      3. parameter the number of bytes we want to write out


Solution:

  1. //make a copy of file given in argv[1] to file given in argv[2]
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h> //open,creat
  5. #include <sys/types.h> //open
  6. #include <sys/stat.h>
  7. #include <errno.h> //perror, errno
  8. int main(int argc,char** argv){
  9.  if (argc!=3){perror("You have to use program with two arguments, the file names copy_from copy_to");exit(1);}
  10.  int f,g;
  11.  f=open(argv[1],O_RDONLY);
  12.  //there is an access function with which we can see whether the file exists or not
  13.  //access(filename,F_OK);
  14.  //open 1. parameter= file name
  15.  //open 2. parameter for what we want to use the file
  16.  //O_RDONLY=only for reading,O_WRONLY-only for writing
  17.  if (f<0){ perror("Error at opening the file\n");exit(1);}
  18.  //There is errno variable in which there is the number of error --
  19.  //if (errno!=0) there is an error
  20.  g=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
  21.  //the three parameter long version of open - it can create the file if it doesnt exist
  22.  //there is a creat function as well - creat(filename,permissions);
  23.  //O_TRUNC = if it existed, clear the content,
  24.  //O_CREAT=create if it doesn't exist
  25.  //3. parameter the permission, if it has to be created
  26.  //S_IRUSR=permission for reading by the owner e.g.
  27.  if (g<0){ perror("Error at opening the file\n");exit(1);}
  28.  char c;
  29.  while (read(f,&c,sizeof(c))){
  30.     //read gives back the number of bytes
  31.     //1. parameter the file descriptor
  32.     //2. parameter the address of variable, we read into
  33.     //3. parameter the number of bytes we want to read in
  34.     printf("%c",c); //we prints out the content of the file on the screen
  35.     if (write(g,&c,sizeof(c))!=sizeof(c))
  36.       {perror("There is a mistake in writing\n");exit(1);}
  37.       //write gives back the number of written bytes
  38.       //1. parameter the file descriptor
  39.       //2. parameter the address of variable we want to write out
  40.       //3. parameter the number of bytes we want to write out
  41.  }
  42.  close(f);
  43.  close(g);
  44.  return 0;
  45. }