1- #include <stdio.h>
2- #include <stdlib.h>
3- #include "struct_sort.h"
4- #include <ctype.h>
1+ /**
2+ * @author Brett Dale
3+ * @version 1.0 (2/28/2020)
4+ *
5+ * This C source file creates a collection of Contacts and sorts them by
6+ * last name first, then first name if last name's are the same.
7+ */
58
6- //COMMENT THEN README
7- //WRITE METHOD SIGNATURES IN HEADER
9+ #include "struct_sort.h" /** Header file for the source file */
810
11+ /**
12+ * This method eliminates all whitespace leading or trailing the string passed
13+ */
914void trim (char * string ) {
1015 char * ptr = string ;
1116 int length = strlen (ptr );
@@ -19,6 +24,12 @@ void trim(char *string) {
1924 memmove (string , ptr , length + 1 );
2025}
2126
27+ /**
28+ * This method copy's the Contact struct passed in and returns the copy
29+ *
30+ * @param contact contact to be copied
31+ * @return Copy of Contact struct passed
32+ */
2233Contact copy (Contact contact ){
2334 Contact temp ;
2435 strcpy (temp .first_name , contact .first_name );
@@ -31,6 +42,13 @@ Contact copy(Contact contact){
3142 return temp ;
3243}
3344
45+ /**
46+ * This method was used in the building process to display all contacts
47+ * in the Contact struct passed
48+ *
49+ * @param contacts contacts array
50+ * @param num number of elements in array
51+ */
3452void print_contacts (Contact contacts [], int num ){
3553 int i = 0 ;
3654 while (i < num ) {
@@ -46,6 +64,13 @@ void print_contacts(Contact contacts[], int num){
4664 printf ("%s\n" , "-------------------------------" );
4765}
4866
67+ /**
68+ * This method sorts the Contact array passed in order of last name
69+ * first, then first name if last names are the same
70+ *
71+ * @param contacts contacts array
72+ * @param num number of elements in array passed
73+ */
4974void sort (Contact contacts [], int num ) {
5075 int EQ = 0 ;
5176 for (int i = 0 ; i < num ; ++ i ) {
@@ -66,6 +91,13 @@ void sort(Contact contacts[], int num) {
6691 }
6792}
6893
94+ /**
95+ * This method reads in file passed in and creates Contact structs that
96+ * eventually go into the contacts array passed
97+ * @param file file to be read from
98+ * @param contacts contacts array to place Contact structs
99+ * @return integer of elements placed into contacts array
100+ */
69101int read_file (FILE * file , Contact contacts []) {
70102 char str [NUM ];
71103 int i = 0 ;
@@ -112,6 +144,12 @@ int read_file(FILE *file, Contact contacts[]) {
112144 return i ;
113145}
114146
147+ /**
148+ * This method writes the sorted contacts array passed in to the file passed in
149+ * @param output file to be written to
150+ * @param contacts array of Contact structs
151+ * @param num number of elements in contacts array
152+ */
115153void write_to_file (FILE * output , Contact contacts [], int num ){
116154 for (int i = 0 ; i < num ; ++ i ) {
117155 fprintf (output , "%s, " , contacts [i ].first_name );
@@ -124,20 +162,22 @@ void write_to_file(FILE *output, Contact contacts[], int num){
124162 }
125163}
126164
165+ /**
166+ * Main method that runs the file
167+ * @param argc number of how many arguments were passed
168+ * @param argv array of command line arguments passed
169+ * @return 0
170+ */
127171int main (int argc , char * argv []){
128172 FILE * file ;
129173 FILE * output ;
130174 Contact contacts [NAME ];
131175 file = fopen (argv [1 ], "r" );
132176 int num = read_file (file , contacts );
133- //printf("%s\n", "\n[BEFORE SORT]\n-------------------------------");
134- //print_contacts(contacts, num);
135177 sort (contacts , num );
136- //printf("%s\n", "\n[AFTER SORT]\n-------------------------------");
137- //print_contacts(contacts, num);
138178 fclose (file );
139- output = fopen (argv [2 ], "w+ " );
179+ output = fopen (argv [2 ], "w" );
140180 write_to_file (output , contacts , num );
141-
181+ fclose ( output );
142182 return (0 );
143- }
183+ }
0 commit comments