Last active
July 20, 2017 14:42
-
-
Save snbk97/bcbac0094fa23373123b1a7154a6c91d to your computer and use it in GitHub Desktop.
struct program in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
//author: snbk97 | |
struct emp | |
{ | |
int emp_code; | |
char emp_name[100]; | |
char emp_date_of_birth[10]; | |
char emp_date_of_joining[10]; | |
char emp_designation[100]; | |
int emp_salary; | |
} employee[10]; | |
int main(int argc, char const *argv[]) | |
{ FILE *fp; | |
char fc; | |
int i; | |
int limit = 1; //change limit to change nos. of loops | |
char name[100],dob[10],doj[10],desg[100]; | |
// FOR DATES USE THIS FORMAT: | |
// DD / MM / YY | |
for (i = 0; i < limit; i++) { | |
printf("\n[Employee %d ]\n", i+1); | |
printf("Enter Employee code: "); | |
scanf("%d", &employee[i].emp_code); | |
getchar(); | |
printf("Enter Employee name: "); | |
fgets(name, 100, stdin); | |
strcpy(employee[i].emp_name, name); | |
printf("Enter Employee DOB: "); | |
fgets(dob, 10, stdin); | |
strcpy(employee[i].emp_date_of_birth, dob); | |
printf("Enter Employee DOJ: "); | |
fgets(doj, 100, stdin); | |
strcpy(employee[i].emp_date_of_joining, doj); | |
printf("Enter Employee designation: "); | |
fgets(desg, 100, stdin); | |
strcpy(employee[i].emp_designation, desg); | |
printf("Enter Employee salary: "); | |
scanf("%d", &employee[i].emp_salary); | |
getchar(); | |
} | |
//writing | |
fp = fopen("employee.txt","w"); | |
for(i = 0; i < limit; i++) { | |
fprintf(fp, "emp_code: %d\n emp_name: %s emp_date_of_birth: %s emp_date_of_joining: %s emp_designation: %s emp_salary: %d\n\n", | |
employee[i].emp_code, | |
employee[i].emp_name, | |
employee[i].emp_date_of_birth, | |
employee[i].emp_date_of_joining, | |
employee[i].emp_designation, | |
employee[i].emp_salary); | |
printf("\n"); | |
} | |
fclose(fp); | |
//reading | |
printf("\n----------------\n"); | |
printf("READ FROM FILE \n"); | |
printf("\n----------------\n"); | |
fopen("employee.txt","r"); | |
if(fp) { | |
while((fc = fgetc(fp)) != EOF){ | |
printf("%c", fc); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment