Hello This is a C program that i just need someone to commen

Hello,

This is a C program that i just need someone to comment and explain what is happening with each funtcion so i can understand what is going on. For example i dont know what the function LOAD file is doing. Also what is fgets and rewind . Please be thorough in your explanation . Thank you

#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.\ \");
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] = {\'\\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;
}

Solution

struct _data *LOAD(FILE *stream, int size-This structure loads,reads data and returns item position.

void SEARCH(struct _data *BlackBox, char *name, int size) - is conducting name search received through command line arguments, after comparing string name from the file, it reports the searched string position in the file.

void FREE(struct _data *BlackBox, int size) shall free the memory space occupied by the item names.

Hello, This is a C program that i just need someone to comment and explain what is happening with each funtcion so i can understand what is going on. For exampl
Hello, This is a C program that i just need someone to comment and explain what is happening with each funtcion so i can understand what is going on. For exampl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site