#include amp;lt;stdio.hamp;gt; #include amp;lt;stdlib.hamp;gt; #include amp;lt;string.hamp;gt; struct _data { char *name; long number; }; int...

#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/zeze/info.txt","r"); // read mode
if( (*stream) == NULL )
{
perror("Error while opening the file.\n");
return 0;
}
while(!feof((*stream)))
{
int ch = fgetc((*stream));
if(ch == '\n')
{
lines++;
}
}
rewind(*stream);
return lines;
}
struct _data *LOAD(FILE *stream, int size){
char line[256] = {'\0'};
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,'\0',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) {
int i=0;
for(i ; 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) {
int i=0;
for(i; 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;
}