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

The code needs to be in C and in a manner where the struct remains unchanged. TH

ID: 3851881 • Letter: T

Question

The code needs to be in C and in a manner where the struct remains unchanged. THANKS! :)

Assignment #3: Structures on Disk The Problem Your task is to write a program that stores and retrieves structures in a file on disk. The file of structures must remain on the disk once your program ends. You should be able to store structures to the file and retrieve from the file after restarting the program The record that you will be writing to file has the following structure: struct contact unsigned long phone number long first name posn; long last name posn long company name posn long email posn ong next; first name posn, ast name posn, company name posn, and ema posn are the position in the file of the First Name, Last Name, Company Name, and Email variable strings. These "locations" will be required for writing and reading the information to be stored when you write the structures on disk you will need to store the First Name, Last Name, Company Name, and Email strings separately from the contact structure. In the file you will write the contact structure first followed by the First Name, Last Name, Company Name, and strings (if they exist The only required information is the Phone Number all other Ema information is optional. The position "next stores the location in the file where the next contact record is stored. The file can contain any number of records and associated strings. The name of the file will be myContactList.db and if the file does not exist then you program must create it. Interface (input and output) Your program will have an input interface that does the following: Do you wish to enter a new contact ies or No)?: First Name: Last Name: Company Name: Phone Number (enter only numbers): Email:

Explanation / Answer

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

typedef struct contact {
    unsigned long phone_number;
    long first_name_posn;
    long last_name_posn;
    long company_name_posn;
    long email_posn;
    long next;
} contact;

/* Reads and writes contact data to disk, stored in a linked list.
While loop #1 is data entry, #2 is search and retrieval (will only
return the first match; works on phone #s). */
int main()
{
    int end = 0;
    int pnSearch = 0;
    int searchCount = 0;
    char continueInput[3];
    char fName[50];
    char lName[50];
    char cName[50];
    char pNum[50];
    char email[50];
    contact contactInput;
    contact searchOutput;
    FILE *fptr = fopen("myContactsList.db", "a+");/*a+ opens for reading and appending*/
  
    end = 0;
    while(end == 0) {
        printf("Do you wish to enter a new contact (Yes or No)?: ");
        fgets(continueInput, 3, stdin);
        if(strcmp("No", continueInput))
        {
            end = 1;
            break;
        }
        else if(!strcmp("Yes", continueInput))
        {
            printf("Enter "Yes" or "No", please. ");
            continue;
        }
        else
        {
            /*set the next of the last one*/
            printf("First Name: ");
            fgets(fName, 50, stdin);
            printf("Last Name: ");
            fgets(lName, 50, stdin);
            printf("Company Name: ");
            fgets(cName, 50, stdin);
            while(1) {
                printf("Phone Number (enter only numbers): ");
                fgets(pNum, 11, stdin);
                if(!isdigit(pNum))
                {
                    printf("Enter only numbers. ");
                    continue;
                }
                else if(strlen(pNum) != 10)
                {
                    printf("Enter a number 10 digits in length. ");
                    continue;
                }
                else
                {
                    break;
                }
            }
            printf("Email: ");
            fgets(email, 50, stdin);
          
            contactInput.phone_number = strtoul(pNum, NULL, 10);
          
            contactInput.first_name_posn = ftell(fptr) + sizeof(contact);
            contactInput.last_name_posn = contactInput.first_name_posn + (sizeof(char) * (strlen(fName) + 1));
            if(fName[0] == '')
            {
                contactInput.last_name_posn = contactInput.first_name_posn;
                contactInput.first_name_posn = 0;
            }
            contactInput.company_name_posn = contactInput.last_name_posn + (sizeof(char) * (strlen(lName) + 1));
            if(lName[0] == '')
            {
                contactInput.company_name_posn = contactInput.last_name_posn;
                contactInput.last_name_posn = 0;
            }
            contactInput.email_posn = contactInput.company_name_posn + (sizeof(char) * (strlen(cName) + 1));
            if(cName[0] =='')
            {
                contactInput.email_posn = contactInput.company_name_posn;
                contactInput.company_name_posn = 0;
            }
            contactInput.next = contactInput.email_posn + sizeof(char) * (strlen(email) + 1));
            if(email[0] == '')
            {
                contactInput.next = = contactInput.email_posn;
                contactInput.email_posn = 0;
            }
          
            fwrite(&contactInput, sizeof(contact), 1, fptr);
            if(contactInput.first_name_posn != 0;)
            {
                fwrite(fName, sizeof(char), strlen(fName) + 1, fptr);
            }
            if(contactInput.last_name_posn != 0)
            {
                fwrite(lName, sizeof(char), strlen(lName) + 1, fptr);
            }
            if(contactInput.company_name_posn != 0)
            {
                fwrite(cName, sizeof(char), strlen(cName) + 1, fptr);
            }
            if(contactInput.email != 0)
            {
                fwrite(email, sizeof(char), strlen(email) + 1, fptr);
            }
          
            continue;
        }
    }
  
    end = 0;
    while(end == 0) {
        printf("Do you wish to retrieve a contact (Yes or No)?: ");
        fgets(continueInput, 3, stdin);
        if(strcmp("No", continueInput))
        {
            end = 1;
            break;
        }
        else if(!strcmp("Yes", continueInput))
        {
            printf("Enter "Yes" or "No", please. ");
            continue;
        }
        else
        {
            while(1) {
                printf("Phone Number (enter only numbers): ");
                fgets(pNum, 11, stdin);
                if(!isdigit(pNum))
                {
                    printf("Enter only numbers. ");
                    continue;
                }
                else if(strlen(pNum) != 10)
                {
                    printf("Enter a number 10 digits in length. ");
                    continue;
                }
                else
                {
                    pnSearch = strtoul(pNum, NULL, 10);
                    break;
                }
            }
          
            while(!feof(fptr)) {
                fseek(fptr, searchCount, SEEK_SET);
                fread(&searchOutput, sizeof(contact), 1, fptr);
                if(searchOutput.phone_number == pnSearch) {
                    /*print*/
                }
                else
                {
                    /*increment*/
                }
            }
          
            continue;
        }
  
    fclose(fptr);
    return 0;
}