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
  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • C++ Questions Please answer quickly! 1. Please use this info to answer next questions -> 2. Please give reasoning 4. Explain why, I think it is 4 but i also think it might be 0. You need to wri...

    C++ Questions Please answer quickly! 1. Please use this info to answer next questions -> 2. Please give reasoning 4. Explain why, I think it is 4 but i also think it might be 0. You need to write a program that uses the speed of light in a vacuum (299792458 meters per second) Realizing that this is a very large value, you run this code to see how large a value you can store as an int, a long...

  • CSCI 112 C Programming Database Lab This lab will focus on the use and manipulation of...

    CSCI 112 C Programming Database Lab This lab will focus on the use and manipulation of structures. Each section of the lab should be designed within its own function, passing parameters as necessary. You are to construct a C program, database.c, that will retrieve and manipulate a company's payroll database, payfile.txt. The data for each employee should be read into a structure containing the following field identifiers: first : 7 characters maximum, initial : 1 character maximum, last : 9...

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