Question

In: Computer Science

code in C Step 1 Write a program that will read in a list of addresses...

code in C

Step 1

  • Write a program that will read in a list of addresses (100 maximum) from a file.
  • The program should read records from a file until an EOF is found.
  • The program must read one character at a time from the file using fgetc.

For example: chrLetter = fgetc( pfilInput );

  • No partial addresses/records will be given.
  • The format of the addresses is: RecordID, Full Name, Street, City, State, ZipCode

Step 2

  • Store the addresses in an array of structures.
  • Use the typedef below
  • You will have to parse the input lines to separate the fields.
  • You will have to separate full name into first, middle and last name.
  • Trim all leading and trailing whitespace for EACH field.

typedef struct

{

      long lngRecordID;

      char strFirstName[ 50 ];

      char strMiddleName[ 50 ];

      char strLastName[ 50 ];

      char strStreet[ 100 ];

      char strCity[ 50 ];

      char strState[ 50 ];

      char strZipCode[ 50 ];

} udtAddressType;

Step 3

  • Call a subroutine from main and pass the array of structures as a parameter. In the subroutine, load all the addresses from the file into the array of structures.

Step 4

  • Call a subroutine from main and pass the array of structures as a parameter. In the subroutine, print out all the addresses. Make it look pretty. Don’t print out blank records. For example:

------------------------------------------------------------

Customer #1

            First Name:          Luke

            Middle Name:      

            Last Name:          Skywalker

            Address:              123 Elm Street

            City:                    Corusant

            State:                  Ohio

            Zip Code:             45202

------------------------------------------------------------

Customer #2

Extra Credit

  • After you read in the addresses call a subroutine to sort the addresses alphabetically by last name, first name. Try the quick sort in the STL (standard template library).

Extra Extra Credit

  • Eliminate any duplicates addresses (just check street and zip code).

Solutions

Expert Solution

#include<stdio.h>
#define MAX 100
typedef struct
{
    long lngRecordID;
    char strFirstName[ 50 ];
    char strMiddleName[ 50 ];
    char strLastName[ 50 ];
    char strStreet[ 100 ];
    char strCity[ 50 ];
    char strState[ 50 ];
    char strZipCode[ 50 ];
}udtAddressType;

//to load the addresses from file in array
int load_address(udtAddressType arr[])
{
   FILE *file;
   file = fopen("addresses.txt","r");
   if(file==NULL)
       printf("Error");
   int i=0,j=0;
   char c;
   while(getc(file) != EOF)
   {  
       fseek( file, -1, SEEK_CUR );
       fscanf(file,"%d",&arr[i].lngRecordID);  
       c = getc(file);
       c = getc(file);
       j=0;
       while(c != ' ' )
       {
           arr[i].strFirstName[j] = c;
           j++;
           c = getc(file);
       }
       c = getc(file);
       j=0;
       while(c != ' ' )
       {
           arr[i].strMiddleName[j] = c;
           j++;
           c = getc(file);
       }
       c = getc(file);
       j=0;
       while(c != ',' )
       {
           arr[i].strLastName[j] = c;
           j++;
           c = getc(file);
       }
       c = getc(file);
       j=0;
       while(c != ',' )
       {
           arr[i].strStreet[j] = c;
           j++;
           c = getc(file);
       }
       c = getc(file);
       j=0;
       while(c != ',' )
       {
           arr[i].strCity[j] = c;
           j++;
           c = getc(file);
       }
       c = getc(file);
       j=0;
       while(c != ',' )
       {
           arr[i].strState[j] = c;
           j++;
           c = getc(file);
       }
       c = getc(file);
       j=0;
       while(c != '\n' )
       {
           arr[i].strZipCode[j] = c;
           j++;
           c = getc(file);
       }
       i++;
   }
   return i;
}

//to print the array
void printAddress(udtAddressType arr[],int size)
{
   int i;
   for( i=0;i<size;i++)
   {
       printf("-----------------------------------------------------------------------\n");
       printf("Customer #%d\n",i+1);
       printf("\t\tFirst Name : %s\n",arr[i].strFirstName);
       printf("\t\tMiddle Name : %s\n",arr[i].strMiddleName);
       printf("\t\tLast Name :   %s\n",arr[i].strLastName);
       printf("\t\tAddress :     %s\n",arr[i].strStreet);
       printf("\t\tCity :        %s\n",arr[i].strCity);
       printf("\t\tState :       %s\n",arr[i].strState);
       printf("\t\tZip Code :    %s\n\n",arr[i].strZipCode);
   }
}

int main()
{
   udtAddressType arr[MAX];
   int size = load_address(arr);
   printAddress(arr,size);
   return 0 ;
}

//Output


Related Solutions

Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT