Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help!! 1. Using Structures, Unions, Files and Dynamic Memory allocation a

ID: 2082722 • Letter: P

Question

Please help!!

1. Using Structures, Unions, Files and Dynamic Memory allocation and all semester’s topics design a Data Management Software on C.

The comma separated value file exampledata.csv contains the fields:

id, first_name, last_name, email, gender, grades

Design a menu allowing the user to retrieve the information by ID, last_name (show multiple if more than one) or email.

The menu MUST also ask the user to add new records at the end of the file The file must save the last version saved date and hour.


2. Using BIT FIELDS, write a C program defining the next registers:
OPTION_REG
INTCON
TMR0
Reset all their bits to zero, than ask the user which register and which bit(s) to change. Save the registers in a File.

Explanation / Answer

1)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct info
{
int id;
char first_name[20];
char last_name[20];
char email[20];
char gender;
};

struct info * load_into_array(struct info **ptr, int *cnt);
int update_record(struct info **ptr, int id, char lname[], char fname[], char mail[], char g);
int main()
{
int choice;
int count = 0,ch;
struct info *infoArray;
int id,i,ret;
char lname[20];
char email[20];
char fname[20];
int file_changed = 0;
char g;
infoArray = load_into_array(&infoArray, &count);
  
do
{
printf("1.Retrieve information by Id 2.Retrieve by last_name 3.Retrieve by email 4.Add record at the end of file 5.Quit ");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the id : ");
fflush(stdout);
scanf("%d", &id);
for (i = 0; i < count; i++)
{
if (id == infoArray[i].id)
{
printf("Record with %d is found ", infoArray[i].id);
}
}
break;
case 2:
printf("Enter the id : ");
scanf("%s", &lname);
for (i = 0; i < count; i++)
{
if (strcmp(lname, infoArray[i].last_name) == 0)
{
printf("Record with %s is found ", infoArray[i].last_name);
}
}
break;
case 3:
printf("Enter the id : ");
scanf("%s", &email);
for (i = 0; i < count; i++)
{
if (strcmp(email, infoArray[i].email) == 0)
{
printf("Record with %s is found ", infoArray[i].email);
}
}
break;
case 4:
printf("Enter the id : ");
scanf("%d", &id);
printf("Enter the last name: ");
scanf("%s", &lname);
printf("Enter the first name: ");
scanf("%s", &fname);
printf("Enter the email: ");
scanf("%s", &email);
printf("Enter the gender: ");
scanf("%c", &g);
ret = update_record(&infoArray, id, lname, fname, email, g);
if (ret < 0)
{
printf("Error updating records ");
}
else
file_changed = 1;
break;
case 5:
printf("Exiting from application.... ");
break;
}
if (file_changed)
{
infoArray = load_into_array(&infoArray, &count);
file_changed = 0;
}

} while (choice !=5 );
}

struct info *load_into_array(struct info **ptr,int *cnt)
{
char fname[20];
char lname[20];
char email[20];
char g,ch;
int id,count = 0,ret;
FILE *fp;
int i = 0;
fp = fopen("exampledata.csv", "r");
if (fp == NULL)
{
cnt = 0;
return NULL;
}
while ((ch = fgetc(fp)) != EOF){
if (ch == ' ')
count++;
}
ptr = (struct info **)malloc(sizeof(struct info*)*sizeof(struct info));
for ( i = 0; i < count; i++)
{
ptr[i] = (struct info *)malloc(sizeof(struct info)*count);
}
fclose(fp);
fp = fp = fopen("exampledata.csv", "r");
while ( i < count )
{
fscanf(fp, "%d%s%s%s%c", &id, &lname, &fname, &email, &g);
ptr[i]->id = id;
strcpy(ptr[i]->last_name, lname);
strcpy(ptr[i]->first_name, fname);
strcpy(ptr[i]->email, email);
ptr[i++]->gender = g;

}
fclose(fp);
*cnt = count;
return *ptr;
}

int update_record(struct info **ptr, int id, char lname[], char fname[], char mail[], char g)
{
FILE *fp;
fp = fopen("exampledata.csv", "W+");

if (fp == NULL)
{
printf("Not able to open file for reading ");
return -1;
}
  
fprintf(fp, "%d,%s,%s,%s,%c ", id, lname, fname, mail, g);
fclose(fp);
return 0;
}