In the following C program, identify what is wrong with code and correct it. This program reads addresses from an input.txt file and sorts the adresses by order of zip codes into an output.txt file. Please also see the following error codes, thank you main.c: #include "contacts.h" #include #include int main(int argc, char **argv) { size_t size; FILE *fp_in, *fp_out; errno_t err; if (argc != 3) { printf("Usage: %s infile outfile", argv[0]); return 0; } err = fopen_s(&fp_in, argv[1], "r"); if (err != 0) { perror("Failed opening file for reading"); } err = fopen_s(&fp_out, argv[2], "w+"); if (err != 0) { perror("Failed opening file for writing"); } contact **c = alloc_contacts(MAX_CONTACTS); size = get_contacts(c, fp_in); if (size == 0) perror("get_contacts failed"); sort_contacts(c, size); print_contacts(c, size, fp_out); free_alloc(c, size); if (fp_in) { err = fclose(fp_in); if (err != 0) { perror("File could not be closed"); } } if (fp_out) { err = fclose(fp_out); if (err != 0) { perror("File could not be closed"); } } return 1; } contacts.c: #include "contacts.h" #include #include /*Creates up to 50 structures*/ void create_data(user_data *users[]) { char new_user[100]; int i = 0; user_data *temp; /*Gets input until no info left*/ while (gets(new_user) != NULL && i < 50) { /*Dynamically Allocates new structure, then copies to array of pointers to structures*/ temp = (user_data*)malloc(sizeof(user_data)); /*Get info from file*/ strcpy(temp->name, new_user); gets(temp->address); gets(temp->city); gets(temp->zipcode); users[i++] = temp; }; users[i] = NULL; } /*Sorts data according the zipcode*/ void sort_data(user_data *users[]) { user_data *temp; /*Sorts smallest number to beginning*/ for (int i = 0; users[i] != NULL; i++) for (int j = i+1; users[j] != NULL; j++) if (strcmp(users[i]->zipcode, users[j]->zipcode) > 0) { temp = users[j]; users[j] = users[i]; users[i] = temp; } } /*Outputs data*/ void print_data(user_data *users[]) { for (int i = 0; users[i] != NULL; i++) printf("%s\n%s\n%s\n%s\n", users[i]->name, users[i]->address, users[i]->city, users[i]->zipcode); } /*Frees dynamically allocated memory*/ void free_mem(user_data *users[]) { for (int i = 0; users[i] != NULL; i++) free((void*)users[i]); } contacts.h: #define CONTACTS_H_INCLUDED typedef struct user_data{ char name[100]; char address[100]; char city[100]; char zipcode[100]; } user_data; void create_data(user_data *users[]); void sort_data(user_data *users[]); void print_data(user_data *users[]); void free_mem(user_data *users[]);
This program reads addresses from an input.txt file and sorts the adresses by order of zip codes into an output.txt file.
Please also see the following error codes, thank you
main.c:
#include "contacts.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
size_t size;
FILE *fp_in, *fp_out;
errno_t err;
if (argc != 3) {
printf("Usage: %s infile outfile", argv[0]);
return 0;
}
err = fopen_s(&fp_in, argv[1], "r");
if (err != 0) {
perror("Failed opening file for reading");
}
err = fopen_s(&fp_out, argv[2], "w+");
if (err != 0) {
perror("Failed opening file for writing");
}
contact **c = alloc_contacts(MAX_CONTACTS);
size = get_contacts(c, fp_in);
if (size == 0)
perror("get_contacts failed");
sort_contacts(c, size);
print_contacts(c, size, fp_out);
free_alloc(c, size);
if (fp_in) {
err = fclose(fp_in);
if (err != 0) {
perror("File could not be closed");
}
}
if (fp_out) {
err = fclose(fp_out);
if (err != 0) {
perror("File could not be closed");
}
}
return 1;
}
contacts.c:
#include "contacts.h"
#include <stdio.h>
#include <stdlib.h>
/*Creates up to 50 structures*/
void create_data(user_data *users[])
{
char new_user[100];
int i = 0;
user_data *temp;
/*Gets input until no info left*/
while (gets(new_user) != NULL && i < 50)
{
/*Dynamically Allocates new structure, then copies to array of pointers to structures*/
temp = (user_data*)malloc(sizeof(user_data));
/*Get info from file*/
strcpy(temp->name, new_user);
gets(temp->address);
gets(temp->city);
gets(temp->zipcode);
users[i++] = temp;
};
users[i] = NULL;
}
/*Sorts data according the zipcode*/
void sort_data(user_data *users[])
{
user_data *temp;
/*Sorts smallest number to beginning*/
for (int i = 0; users[i] != NULL; i++)
for (int j = i+1; users[j] != NULL; j++)
if (strcmp(users[i]->zipcode, users[j]->zipcode) > 0)
{
temp = users[j];
users[j] = users[i];
users[i] = temp;
}
}
/*Outputs data*/
void print_data(user_data *users[])
{
for (int i = 0; users[i] != NULL; i++)
printf("%s\n%s\n%s\n%s\n", users[i]->name, users[i]->address, users[i]->city, users[i]->zipcode);
}
/*Frees dynamically allocated memory*/
void free_mem(user_data *users[])
{
for (int i = 0; users[i] != NULL; i++)
free((void*)users[i]);
}
contacts.h:
#define CONTACTS_H_INCLUDED
typedef struct user_data{
char name[100];
char address[100];
char city[100];
char zipcode[100];
} user_data;
void create_data(user_data *users[]);
void sort_data(user_data *users[]);
void print_data(user_data *users[]);
void free_mem(user_data *users[]);
![Project8 - Microsoft Visual Studio
Quick Launch (Ctrl+Q)
File
Edit
View
Project
Build
Debug
Team
Tools
Test
Analyze
Window
Help
gerardo chavez
GC
Debug
x86
> Local Windows Debugger
contacts.h
main.c + X contacts.C
Solution Explorer
* Project8
(Global Scope)
e O A 6. -
30
free_alloc(c, size);
Search Solution Explorer (Ctrl+;)
31
if (fp_in) {
err = fclose(fp_in);
if (err != 0) {
32
Solution 'Project8' (1 project)
* Project8
-I References
G External Dependencies
33
34
91 %
Error List
Header Files
Entire Solution
X 13 Errors
A 0 of 9 Warnings
O O Messages
Build + IntelliSense
A contacts.h
Search Error List
Resource Files
Code
Description
Project
File
Line Sup
A Source Files
abc E0020
identifier "contacts" is undefined
Project8
main.c
24
C contacts.c
E0020
identifier "c" is undefined
Project8
main.c
24
c main.c
abc E0020
identifier "MAX_CONTACTS" is undefined
Project8
main.c
24
X C2065
'contacts': undeclared identifier
Project8
main.c
24
* C2065
'c: undeclared identifier
Project8
main.c
24
* C2100
illegal indirection
Project8
main.c
24
X C2065
"MAX_CONTACTS': undeclared identifier
Project8
main.c
24
X C2106
'=': left operand must be l-value
'c': undeclared identifier
Project8
main.c
24
X C2065
Project8
main.c
25
8 C2065
'c: undeclared identifier
Project8
main.c
Solution Explorer Team Explorer
28
* C2065
'c': undeclared identifier
Project8
main.c
29
Properties
X C2065
8 C1075
'c: undeclared identifier
Project8
main.c
30
'{': no matching token found
Project8
main.c
Activate Windows
Go to Settings to activate Windows.
Error List Output
O Ready
Ln 30
Col 1
Ch 1
INS
1 Add to Source Control
3:38 PM
0 Type here to search
^ [a ENG
25
12/12/2020
20
Server Explorer Toolbox](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6c6c0c23-4c95-495d-b7f2-68c0498b48de%2F2720df23-8759-4885-b921-320d4f0859d4%2Fqkqiszg_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)