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[]);

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question
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 <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
Transcribed Image Text: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
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education