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

C PROGRAMMING LANGUAGE The program will require the following structure: struct

ID: 3793437 • Letter: C

Question

C PROGRAMMING LANGUAGE

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 be formatted as:

ron 7774013

jon 7774014

tom 7774015

won 7774016

HINTS:

------

Functions that will make things much easier:

getline()

feof()

strtok()

atoi()

SAMPLE RUNS:

------------

Case 1 - No command line argument provided.

[yourname@chef junk]$ ./CS230-5

*******************************************

* You must include a name to search for. *

*******************************************

Case 2 - Provided name is NOT in the list.

[yourname@chef junk]$ ./CS230-5 joe

*******************************************

The name was NOT found.

*******************************************

Case 3 - Provided name is in the list.

*******************************************

The name was found at the 2 entry.

*******************************************

Explanation / Answer

Code will be look like

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

structure _data
{   
char *Namefirst;
char *Namelast;
long nbr;
};

// SCANINGNING FILE
int SCANING(FILE *(*strm))
{
*strm = fopen("inputFile.data", "r");

int ch = 0, lines = 0;

while (!feof(*strm))
{
ch = fgetc(*strm);
if (ch == ' ')
{
lines++;
}
}
return lines;
}

// LOADING FILE
structure _data *LOADING(FILE *strm, int size)
{
int i;
size_t CharacterCount;
char *text, *nbr, *Namefirst, *Namelast;
structure _data *BlkBox;

if ((BlkBox = (structure _data*)calloc(size, sizeof(structure _data))) == NULL)
{
printf("Warning - Could not allocate memory. ");
exit(0);
}

rewind(strm);


for (i = 0; i < size; i++)
{
getline(&text, &CharacterCount, strm);
Namefirst = strtok(text, " ");
Namelast = strtok(text, " ");
nbr = strtok(NULL, " ");

// Allocate memory for name part of struct.
if ((BlkBox[i].Namefirst = (char*)calloc(strlen(Namefirst), sizeof(char))) == NULL)
{
printf("ERROR - Could not allocate memory. ");
exit(0);
}
if ((BlkBox[i].Namelast = (char*)calloc(strlen(Namelast), sizeof(char))) == NULL)
{
printf("Warning - Could not allocate memory. ");
exit(0);
}

strcpy(BlkBox[i].Namefirst, Namefirst);
strcpy(BlkBox[i].Namelast, Namelast);
BlkBox[i].nbr = atol(nbr);
}
fclose(strm);
return BlkBox;
}


void SEARCH(structure _data *BlkBox, char *name, int size, int inputs)
{
int i;
int found = 0;
char *search = " ";
char *Namefirst;
char *Namelast;

if (inputs == 2)
{
Namefirst = strtok(name, search);
Namelast = strtok(NULL, search);
}


printf("******************************************* ");
if (inputs == 2)
{   
for (i = 0; i < size; i++)
{
if (!strcasecmp(Namefirst, BlkBox[i].Namefirst) && !strcasecmp(Namefirst, BlkBox[i].Namefirst))
{
printf("The name was found at the %d entry. ", i);
found = 1;
break;
}
}
}
else
{
for (i = 0; i < size; i++)
{
if (!strcasecmp(Namefirst, BlkBox[i].Namefirst) || !strcasecmp(Namefirst, BlkBox[i].Namefirst))
{
printf("The name was found at the %d entry. ", i);
found = 1;
break;
}
}
}

if (found == 0)
{
printf("The name was NOT found. ");
}
printf("******************************************* ");
}

// FREE MEMORY
void FREE(structure _data *BlkBox, int size)
{
int i;
for (i = 0; i < size; i++)
{
free(BlkBox[i].Namefirst);
free(BlkBox[i].Namelast);
}
free(BlkBox);
BlkBox = NULL;
}


// MAIN
int main(int argv, char **argc)
{
int size;
FILE *strm;
structure _data *BlkBox;

// argv == 1 WORKS, Below message is printed.
if (argv == 1)
{
printf("******************************************* ");
printf("* You must include a name to search for. * ");
printf("******************************************* ");
}
// argv == 2 DOES NOT WORK, Segmentation Fault.   
if (argv == 2)
{
size = SCANING (&strm);
BlkBox = LOADING(strm, size);
SEARCH(BlkBox, argc[1], size, 1);
}
if (argv == 3)
{
size = SCANING(&strm);
BlkBox = LOADING(strm, size);
SEARCH(BlkBox, argc[2], size, 2);
}
return 0;
}