Question

Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following...

Programing In C

Add Command Line Arguments (Argc and Argv) and use files to the following program.

My program is called Ultimo.exe and it is in (C:\Users\Dell\source\repos\Ultimo\Debug). The file used is a .txt file in the directory of the .exe program.

The text file which is address.txt is at the same location as Ultimo.exe (C:\Users\Dell\source\repos\Ultimo\Debug). The program takes information in the address.txt file and sorts it by zip code, from smallest to largest. The program works by using input/output redirection using the CMD prompt. To make the program work I open a cmd and go to the directory of Ultimo.exe, there I type " Ultimo.exe<address.txt" and the addresses get sorted in the cmd.

What needs to be added to this program is Command Line Arguments (Argc and Argv). Argv[0] should be the program, Argv[1] should be the address.txt and Argv[2] should be an output.txt file.

Here is the program:

----------------------------------------------------------------------------------------------------------Main.c-----------------------------------------------------------------------------------------------

#include "Header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
   information *info[50];
int n = input(info);
   outputting(info, n);

return 0;
}

------------------------------------------------------------------------------------------------------Header.h-----------------------------------------------------------------------------------------------

#pragma once
#pragma once
#ifndef HEADER_H
#define HEADER_H
#define size 40

typedef struct
{
   char name[size];
   char address[size];
   char citystate[size];
   char zipcode[size];
}information;


void sortZipcodes(information *[], int);
#endif HEADER_H

----------------------------------------------------------------------------------------functions.c----------------------------------------------------------------------------------------------

#include "Header.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char buffer[81];
typedef information *pointer;
pointer ptr;

int input(information *info[])
{
   int pointer_num = 0;
   int index = 0;
   info[index] = (information *)malloc(sizeof(information));


   while ((gets(buffer) != NULL) && (pointer_num < 50))
   {
       ptr = (pointer)malloc(sizeof(information));
       strcpy(ptr->name, buffer);
       gets(ptr->address);
       gets(ptr->citystate);
       gets(ptr->zipcode);
       index++;

       info[pointer_num++] = ptr;
   }

   index--;
   sortZipcodes(info, index);
   return pointer_num;
}


int getZipCode(char info[])
{
   int zip = atoi(info);
   return zip;

}


void sortZipcodes(information *info[], int index)
{
   for (int i = 0; i <= index; i++)
   {
       for (int c = i + 1; c <= index; c++)
       {

           int min = getZipCode(info[i]->zipcode);
           information *srt = NULL;

           if (min > getZipCode(info[c]->zipcode))
           {
               srt = info[i];
               info[i] = info[c];
               info[c] = srt;
           }

       }
   }
}

void outputting(information *info[], int num)
{
   for (int i = 0; i < num; i++)
   {

       puts((*info[i]).name);
       puts((*info[i]).address);
       puts((*info[i]).citystate);
       puts((*info[i]).zipcode);
   }
   for (int i = 0; i <= 50; i++)
   {
       free((void*)info[i]);
   }
   return 0;
}

-------------------------------------------------------------------------------------------address.txt----------------------------------------------------------------------------------------------

A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
C1, C2
5142 Dumont Pl
Azusa, CA
91112
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
F1, F2
20225 Lorenzana Dr
Los Angeles, CA
91111
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
J1, J2
5135 Quakertown Ave
Thousand Oaks, CA
91362
K1, K2
720 Eucalyptus Ave 105
Inglewood, CA
89030
L1, L2
5021 Dumont Pl
Woodland Hills, CA
91364
M1, M2
4819 Quedo Pl
Westlake Village, CA
91362
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
N1, N2
20044 Wells Dr
Beverly Hills, CA
90210
O1, O2
7659 Mckinley Ave
Los Angeles, CA
90001
--------------------------------------------------------------------------------------------------Thank You----------------------------------------------------------------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Modified program to take the command line argumnet, Program takes filename as command line argument instead of taking input from user.. Made changes to Main.c and functions.c file

//Header.h, no change

#pragma once

#pragma once

#pragma once

#ifndef HEADER_H

#define HEADER_H

#define size 40

typedef struct

{

  char name[size];

  char address[size];

  char citystate[size];

  char zipcode[size];

}information;


void sortZipcodes(information *[], int);

#endif HEADER_H

===============================

//functions.c, changed input function to take file name as second parameter to function , you can find my changes with keyword CHEGGEA

#define _CRT_SECURE_NO_WARNINGS

#include "Header.h"

#include <stddef.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


char buffer[81];

typedef information *pointer;

pointer ptr;

//CHEGGEA , changed function to take the file as additional parameter

int input(information *info[], char *filename)

{

  //CHEGGEA, declare file pointer to read from input file, address.txt

  FILE *fp;

  int pointer_num = 0;

  int index = 0;

  info[index] = (information *)malloc(sizeof(information));

  //CHEGGEA , open file for reading

  fp = fopen(filename, "r");

  //check if file is open

  if (fp == NULL)

  {

    printf("Not able to open %s for reading\n", filename);

    return -1;

  }

  //while ((gets(buffer) != NULL) && (pointer_num < 50))

  //read till EOF ,CHEGGEA

  while (!feof(fp))

  {

    ptr = (pointer)malloc(sizeof(information));

    /*strcpy(ptr->name, buffer);

    gets(ptr->address);

    gets(ptr->citystate);

    gets(ptr->zipcode);*/

    fgets(ptr->name, sizeof(ptr->name), fp);

    ptr->name[strlen(ptr->name) - 1] = '\0';

    fgets(ptr->address, sizeof(ptr->address), fp);

    ptr->address[strlen(ptr->address) - 1] = '\0';

    fgets(ptr->citystate, sizeof(ptr->citystate), fp);

    ptr->citystate[strlen(ptr->citystate) - 1] = '\0';

    fgets(ptr->zipcode, sizeof(ptr->zipcode), fp);

    ptr->zipcode[strlen(ptr->zipcode) - 1] = '\0';

    index++;

    info[pointer_num++] = ptr;

  }

  index--;

  sortZipcodes(info, index);

  return pointer_num;

}


int getZipCode(char info[])

{

  int zip = atoi(info);

  return zip;

}


void sortZipcodes(information *info[], int index)

{

  for (int i = 0; i <= index; i++)

  {

    for (int c = i + 1; c <= index; c++)

    {

      int min = getZipCode(info[i]->zipcode);

      information *srt = NULL;

      if (min > getZipCode(info[c]->zipcode))

      {

        srt = info[i];

        info[i] = info[c];

        info[c] = srt;

      }

    }

  }

}

void outputting(information *info[], int num)

{

  for (int i = 0; i < num; i++)

  {

    puts((*info[i]).name);

    puts((*info[i]).address);

    puts((*info[i]).citystate);

    puts((*info[i]).zipcode);

  }

//CHEGGEA , here there was program, program as run time error as i was checking against <=50 instead of < num , changed that

  for (int i = 0; i <num; i++)

  {

    free((void*)info[i]);

  }

  return 0;

}

===========================

//Main.c

#define _CRT_SECURE_NO_WARNINGS

#include "Header.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


int main(int argc , char **argv)

{

  information *info[50];

if(argc < 2)

{

printf("Usage: %s inputfilename\n",argv[0]);

return -1;

}

//printf("%s\n",argv[1]);

  int n = input(info,argv[1]);

  outputting(info, n);

  return 0;

}

===========================

//Output without giving command line args , we get output as below

 ./a.out
Usage: ./a.out inputfilename

//Output when filename is given which is noyt present , we get below output

 ./a.out input.txt
Not able to open input.txt for reading

//Output when correct filename is given , output is given below(Output is sorted by zipcode

./a.out address.txtO1, O2
7659 Mckinley Ave
Los Angeles, CA
9000
K1, K2720 Eucalyptus Ave 105
Inglewood, CA
89030
G1, G220253 Lorenzana Dr
Los Angeles, CA
90005
N1, N2
20044 Wells DrBeverly Hills, CA
90210
H1, H2
5241 Del Moreno Dr
Los Angeles, CA91110
F1, F2
20225 Lorenzana Dr
Los Angeles, CA
91111
C1, C2
5142 Dumont Pl
Azusa, CA
91112
J1, J2
5135 Quakertown Ave
Thousand Oaks, CA
91362
M1, M2
4819 Quedo Pl
Westlake Village, CA
91362
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
L1, L2
5021 Dumont Pl
Woodland Hills, CA
91364
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135

Add a comment
Know the answer?
Add Answer to:
Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT