Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming The program will require the following structure: struct _data { c

ID: 3792054 • Letter: C

Question

 C programming   The program will require the following structure:  struct _data {                                     char *name;    long number; };  The program will require command line arguments:  int main(int argv, char **argc) {          Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch  any case where no command line arguement was provided and print a warning message (see below).  You MUST include/use the following functions, defined as follows:  int SCAN(FILE *(*stream)) - this function will open the file/stream  and return an integer indicating how many lines there are. Note that I need to pass stream, which is a pointer, by reference. So I am  passing this as a pointer to a pointer.  struct _data *LOAD(FILE *stream, int size) - this function will  rewind file, create the dynamic array (of size), and read in the  data, populating the _data struct dynamic array. Note that stream  is passed by value this time. The function then returns the populated  array of struct.  void SEARCH(struct _data *BlackBox, char *name, int size) - this function will get the dynamic array of struct passed to it, the name we are looking for, and the size of the array. This function will then search the dynamic array for the name. See below for examples.  void FREE(struct _data *BlackBox, int size) - this function will free up all of the dynamic memory we allocated. Take note of the number of times  malloc/calloc were called, as you need to free that same number.  Finally, the data file will be called hw5.data and will beformated as:  ron 7774013 jon 7774014 tom 7774015 won 7774016  HINTS: ------ Functions that will make things much easier: getline() feof() strtok() atoi()  * Make sure you read the man pages for each. They have some "gotchas".  SAMPLE RUNS: ------------ Case 1 - No command line argument provided.  [rmarsh@chef junk]$ ./CS230-5 ******************************************* * You must include a name to search for.  * *******************************************  Case 2 - Provided name is NOT in the list.  [rmarsh@chef junk]$ ./CS230-5 joe ******************************************* The name was NOT found. *******************************************  Case 3 - Provided name is in the list.  [rmarsh@chef junk]$ ./CS230-5 tom ******************************************* The name was found at the 2 entry. ******************************************* 

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _data {
   char *name;
   long number;
};
int SCAN(FILE *(*stream)) {
    int lines = 0;
    (*stream) = fopen("/home/ramkumar/Study/Chegg/1115.txt","r"); // read mode
    if( (*stream) == NULL )
    {
          perror("Error while opening the file. ");
          return 0;
    }
    while(!feof((*stream)))
    {
      int ch = fgetc((*stream));
      if(ch == ' ')
      {
        lines++;
      }
    }
    rewind(*stream);
    return lines;
}
struct _data *LOAD(FILE *stream, int size){
    char line[256] = {''};
   struct _data * items = (struct _data *)malloc(size*(sizeof(struct _data)));
   int index = 0;;
   while (fgets(line, sizeof(line), stream) != NULL){
       index++;
       if(index == size){
           break;
       }
       //Break each line by spaces
       char * name = strtok(line," ");
       //get name
       int nameLenght = strlen(name);
       struct _data * item = (items + index);
       item->name = (char *) malloc(nameLenght+1);
       memset(item->name,'',nameLenght+1);
       strcpy(item->name,name);

       //get number
       item->number = atol(line+nameLenght+1);

   }
   return items;
}
void SEARCH(struct _data *BlackBox, char *name, int size) {
    for(int i = 0 ; i < size ; i++){

        if( strcmp( (BlackBox + i)->name,name) == 0){
            printf("he name was found at the %d entry.", (i+1));
            return;
        }
    }
    printf("The name was NOT found.");
}
void FREE(struct _data *BlackBox, int size) {
    for(int i = 0 ; i < size ; i++){
        struct _data * item = (BlackBox + i);
        //Free Name String
        free (item->name);
        //Free item
        free(item);
    }
}

int main(int argc, char *argv[])
{
    if(argc ==0){
        printf("* You must include a name to search for. *");
        return 0;
    }
    FILE * fp;
    SCAN(&fp);
    struct _data * items = LOAD(fp,4);
    SEARCH(items,argv[0],4);
    FREE(items,4);
    return 0;
}