Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Program for Data Input and Validation of Student Records, Essays (high school) of Advanced Operating Systems

This c program reads and validates the input of social security numbers (ssn) and home-phone numbers of students, checks if the ssn is already registered, and writes the new record to files 'dept.dat' and 'emps.dat'.

What you will learn

  • How does the C program validate the length of the Social Security Number (SSN) and home phone number?
  • How does the C program ensure that the entered SSN is not already registered in the system?
  • What files does the C program write the employee data to?

Typology: Essays (high school)

2014/2015

Uploaded on 10/01/2015

aakash_patel
aakash_patel 🇬🇧

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(void)
{ FILE *f, *fi, *fp;
int p[2], n=0, i, pid, pid2, status, flag = 0, pos = 5, len=0;
long int ssn, phone;
char nm[80], department[20];
Loop: int opt;
if ( flag == -1 )
printf("\nPlease make sure that:\nSSN is 9 digits\nHome-Phone is 10 digits\n\n");
printf("Select one option\n1. New Record\n2. Exit\n Enter your choice:");
scanf("%d", &opt);
switch(opt)
{
case 1: printf("\nSocial Security Number(nine digits number):");
scanf("%ld", &ssn);
printf("\nName:");
scanf("%s", nm);
printf("\nDepartment:");
scanf("%s", department);
printf("\nHome Phone(10 digits number):");
pf3
pf4
pf5

Partial preview of the text

Download C Program for Data Input and Validation of Student Records and more Essays (high school) Advanced Operating Systems in PDF only on Docsity!

#include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/wait.h>

int main(void) { FILE *f, *fi, *fp; int p[2], n=0, i, pid, pid2, status, flag = 0, pos = 5, len=0; long int ssn, phone; char nm[80], department[20]; Loop: (^) if ( flag == -1 )int opt; printf("\nPlease make sure that:\nSSN is 9 digits\nHome-Phone is 10 digits\n\n"); printf("Select one option\n1. New Record\n2. Exit\n Enter your choice:"); scanf("%d", &opt);

switch(opt) { case 1: printf("\nSocial Security Number(nine digits number):"); scanf("%ld", &ssn); printf("\nName:"); scanf("%s", nm); printf("\nDepartment:"); scanf("%s", department); printf("\nHome Phone(10 digits number):");

scanf("%ld", &phone); break; case 2: exit(2); }

int name_len = strlen(nm); int dept_len = strlen(department);

pipe(p); /* setting up the pipe / if ((pid = fork()) ==0) / in child / { //printf("\nChild 1 fork successful and the pid for the child is: %d\n", getpid()); close(p[1]); / child closes p[1] */ dup2(p[0], 0); close(p[0]); read(p[0], &ssn, sizeof(ssn)); read(p[0], nm, name_len); read(p[0], &phone, sizeof(phone)); read(p[0], department, dept_len); long int ssn_len_check = ssn, phone_len_check = phone; while(ssn_len_check != 0) { ssn_len_check = ssn_len_check/10; len++; } if(len != 9) {

goto Loop; } else {pos = pos + 9; Loop1: while(!feof(fi)) { c = fgetc(fi); pos++; if(c == '\n') { pos = pos + 6; fseek(fi, pos, SEEK_SET); fgets(sn, sizeof(sn)+2, fi); ssn1 = atoi(sn); if(ssn1 == ssn) { printf("\nThe SSN entered is already registered with us.\n\n"); goto Loop; } pos = pos + 8; } } } fclose(fi); } f=fopen("emps.dat", "a"); fprintf(f ,"SSN:\t%ld \t\tName:\t%s \t\tHome-Phone:\t%ld \n",ssn,nm,phone);

fclose(f); fp=fopen("dept.dat", "a"); fprintf(fp ,"SSN:\t%ld \t\tDepartment: \t %s \n",ssn, department); fclose(fp); _exit(0); /* child terminates */ }

if ((pid2 = fork ()) == 0) /* child two */

{ printf("\nChild 2 fork successful and the pid for the child is: %d\n", getpid()); close(p[0]); dup2(p[1],1); /* 1 becomes a duplicate of p[1] / close(p[1]); write(p[1], &ssn, sizeof(ssn)); write(p[1], nm, strlen(nm)); write(p[1], &phone, sizeof(phone)); write(p[1], department, strlen(department)); } close(p[0]); close(p[1]); / finished writing p[1] */

while (wait(&status)!=pid); /* waiting for pid */ if (status == 0) printf("child finished\n"); else printf("child failed\n"); return(0); }