Question

Q6. [40 MARKS] Write and document a complete C program to satisfy the following requirements. Given a sequential text file th

// includes [1 mark]

// structure definition [5 marks]

// global variables [2 marks]

// function prototypes [2 marks]

// main function (no menu required – just invoke the function calls to // satisfy the requested program sequence) [5 marks]

// Code all necessary functions [25 marks]

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include<string.h>
#include<stdio.h>
#include <stdlib.h>
struct player
{
unsigned int id;
char name[20];
char pName[20];
float salary;
struct player *next;
};

void ReadFile(struct player **);
void WriteFile(struct player *);
struct player * add_node(struct player *list, struct player *node);
void printReverse(struct player * head) ;

int main()
{
   struct player *head=NULL;
  
   ReadFile(&head);
   //insertionSort(head);
   WriteFile(head);
   printReverse(head) ;
   return 0;
}


void ReadFile(struct player **head)
{
   FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
   struct player *temp, *iter=*head;
   fp = fopen("player.dat", "r");
if (fp == NULL)
return ;

while ((read = getline(&line, &len, fp)) != -1) {
temp = (struct player *) malloc(sizeof(struct player));
      
       sscanf(line, "%d %s %f", &temp->id, &temp->name, &temp->salary);
//       printf("Retrieved line of length %d %s %f:\n", temp->id, temp->name, temp->salary);
      
       processName(temp);
//       printf("check\n");
       *head = add_node(*head, temp);  

}
  
fclose(fp);
if (line)
free(line);
}


processName(struct player *temp)
{
   char first_name[20]={0};
   char last_name[20]={0};
  
   int i=0,j=0;
   //printf("%s\n",temp->name);
   while(temp->name[i] != '_' )
   {
       first_name[i] = temp->name[i];
       i++;
   }
   first_name[i]='\0';
   while(temp->name[i] !='\0')
   {
       last_name[j]=temp->name[i] ;
       i++;j++;
   }
   last_name[j]='\0';
  
  
   strcpy(temp->pName, last_name);
   strcat(temp->pName, first_name);
  
}


void WriteFile(struct player *head)
{
  
  
   char rp[100]={0};
   struct player *temp=head;
   FILE *outfile;
  
// open file for writing
outfile = fopen ("person.dat", "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opend file\n");
return;
  
}
  
  
   if(temp == NULL)
           printf("\nNo data\n");
   while(temp!=NULL)
   {
       fprintf(outfile, "%d %s %.2f\n",temp->id, temp->name, temp->salary);
      
   //   printf("%s\n", temp->pName);
       temp=temp->next;
   }
  
   fclose (outfile);
  
  
   remove("player.dat");
   rename("person.dat","player.dat");
}


struct player * add_node(struct player *list, struct player *node){
   struct player *prev, *next;
   if (!list) {
       list = node;
   }
   else {
       prev = NULL;
       next = list;
       while (next && strcmp((node)->pName, next->pName) >0 ){//compare_node(node,next)>0) {
           prev = next;
           next = next->next;
       }
       if (!next) {
           prev->next = node;
       }
       else {
           if (prev) {
               node->next = prev->next;
               prev->next = node;
           }
           else {
               node->next = list;
               list = node;
           }
       }
   }
   return list;
}

void printReverse(struct player * head)
{
// Base case
if (head == NULL)
return;
  
// print the list after head node
printReverse(head->next);
  
// After everything else is printed, print head
// cout << head->data << " ";
   printf("%d %s %.2f\n",head->id, head->name, head->salary);
}

Add a comment
Know the answer?
Add Answer to:
// includes [1 mark] // structure definition [5 marks] // global variables [2 marks] // function...
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
  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • Question2 uses structured design implemented in C. Array of records (structs) with file I/O is needed....

    Question2 uses structured design implemented in C. Array of records (structs) with file I/O is needed. The program takes two inputs at a time. The name of a person, and, the coin value as an integer in the range 5 to 95. Input coin values should always be divisible by 5 (integer division). Names are one word strings. An example input is: Jane 30 This input line indicates that 30 cents change is to be given to Jane. Output change...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • c++ can use if else, loop,function ,array,file i.o. Can not use swtich, global variables etc..   Please...

    c++ can use if else, loop,function ,array,file i.o. Can not use swtich, global variables etc..   Please do all parts. previously I post 3 time for part1 ,2 and 3 but I can't make it into one complete program code. I even try to post a questions ask and post the 3 parts of code for experts to put it together. But experts said it's not enough information. please help... Thank you ! Program Objectives: Allow students to create arrays Allow...

  • java Data Structures (CIS M. Lowenthal ASSIGNMENT1-BEVIEW The "ABC Hardware Company has hired you to wrte...

    java Data Structures (CIS M. Lowenthal ASSIGNMENT1-BEVIEW The "ABC Hardware Company has hired you to wrte a program for It's Accounts Receivable department (AR a accounts that owe money to the company because they have purchased tems and have not yet paid torthem There are two types of Input data 1) A master file in ascending order by customer number (austomer numbers are 4 dgits lonc) contains a The tle aho 20 character customer name and a balance due. 2)...

  • C++ ONLY The question is - The US Census Bureau gathers population information and aggregates it...

    C++ ONLY The question is - The US Census Bureau gathers population information and aggregates it at the city, county and state level. A research project on population distribution in the Southeast US has acquired a data file from the Census Bureau that contains the populations of each county in Mississippi (MS) and Florida (FL). For this research project write a program that calculates the average county population for these two states. Each line in the Census Bureau data file...

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