Question

C programming The program will require the following structure: struct _data { char *name; long number;...

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.
*******************************************
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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.\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) {
    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;
}

Add a comment
Know the answer?
Add Answer to:
C programming The program will require the following structure: struct _data { char *name; long number;...
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
  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • 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...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • C programming How to get rid of the below error. I do not know why is...

    C programming How to get rid of the below error. I do not know why is this happening and how can I correct it. Please help. typedef struct circleroute{ char* name; } Circle; Circle* circlemalloc(int nroutes); //error free Circle* readRoute(FILE* fin); //error free int main(int argc, char* argv[]){ if(argc < 2){ if(argv[1] != NULL){ FILE *fin = fopen(argv[1], "r"); if(fin == NULL){ printf("Unable to open file %s!\n", argv[1]); return EXIT_FAILURE; } int routes; fscanf(fin, "%d", &routes); Circle* c1= circlemalloc(routes); for(int...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; };...

    Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; }; void func(struct student stud); int main() { struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0; } Abdelghani Bellaachia, CSCI 1121 Page: 16 void func(struct student astud) { printf(" Id is: %d \n", astud.id); printf(" Name is: %s \n", astud.name); printf(" Grade is: %c \n", astud.grade); } Modify this program to include the address of a student as a separate structure....

  • 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...

  • Question 11 pts Fill in the blank. Two functions are defined main.c and MyAge.c   // FILE...

    Question 11 pts Fill in the blank. Two functions are defined main.c and MyAge.c   // FILE main.c main ( ) { _______ int age ; printf ( " the value of age is %d \n", age ) ; } // FILE MyAge.c int age = 10; int MyAge ( ) { } Group of answer choices static external internal extern Flag this Question Question 21 pts Fill in the blank. Two functions are defined main.c and MyAge.c   The below program...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

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
ADVERTISEMENT