Question

In: Computer Science

This is in C code. Below is a list of actions to be performed on a...

This is in C code.

Below is a list of actions to be performed on a file. Assume the following structure is used to store data in the file:

struct person {

char lastName[15];

char firstName[15];

char age[4]; };

Write a program that creates and opens a file called nameage.dat and create functions that perform the following actions. Call these functions from the main method: Assume the file empty, and create 100 records with the following data: lastName = "unassigned" firstname = "" age = "0" Prompt the user for 10 last names, first names, and ages and write them to the file Read the file, from the beginning. Prompt the user for a last name, first name, and age. Update the 4th record with the information provided by the user. Delete the 109th record by setting the fields to the following values: lastName = "unassigned" firstname = "" age = "0" Requirements You will include comments at the start of your program with your name, date written, and purpose of the program. Create the structure identified in the description. Create the four methods identified in the description. Create and open the file in your main method. Call your four methods, in the order they appear in the description. Make sure your file is called nameage.dat Prompt the user for data as required. Use the test cases as your guide. Hints To make the assignment easy use a random-access function with binary data. After the first 100 empty records are created, please append the entries for the second function. Repl.it may tell you your program passes the tests, however, Repl.it can't inspect the contents of the file. It will not be able to tell you if your file is set up correctly. I will have to manually grade that. Use the user interaction shown in the test cases to set up your prompts for user information. I recommend using puts() to display the prompt to the user. Assume the file is to be written to the local directory. You do not need and should not include any path information when opening the file.


What additional information is required? This was all of the information given to me.

Solutions

Expert Solution

dummy file generator  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
// a struct to read and write
struct person  
{
int age;
char firstName[20];
char lastName[20];
};
  
int main ()
{
FILE *fd;
  
// open file for writing
fd = fopen ("nameage.dat", "w");
if (fd == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}
int i=0;
for(i=0;i<100;i++){
struct person obj = {0, "", "unassigned"};

  
// write struct to file
fwrite (&obj, sizeof(struct person), 1, fd);
}
  
fclose (fd);
  
return 0;
}

modifying file code (changing 109th entry)



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
// a struct to read and write
struct person  
{
int age;
char firstName[20];
char lastName[20];
};
  
int main ()
{
FILE *fd;
struct person obj = {0,"","unassigned"};
// open file for writing
fd = fopen ("nameage.dat", "a");
if (fd == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}

int i=0;
for(i=0;i<10;i++){
scanf("%s",&obj.firstName);
scanf("%s",&obj.lastName);
scanf("%d",&obj.age);
// write struct to file
fwrite (&obj, sizeof(struct person), 1, fd);
}
fclose (fd);
  
//modifiyng 109th entry
FILE *fd1;
struct person obj1;
// open file for writing
fd1 = fopen ("nameage.dat", "r+");
if (fd1 == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}

i=0;
for(i=0;i<108;i++){
fread(&obj1, sizeof(struct person), 1, fd1);
}
obj1.age=0;
strcpy(obj1.firstName,"");
strcpy(obj1.lastName, "unassigned");  

// write struct to file
fwrite (&obj1, sizeof(struct person), 1, fd1);
fclose (fd1);

  
return 0;
}

reading file code


#include <stdio.h>
#include <stdlib.h>
  
struct person  
{
int age;
char firstName[20];
char lastName[20];
};
  
int main ()
{
FILE *fd;
struct person obj;
  
// Open person.dat for reading
fd = fopen ("nameage.dat", "r");
if (fd == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
  
// read file contents till end of file
while(fread(&obj, sizeof(struct person), 1, fd))
printf ("age = %d name = %s %s\n", obj.age,
obj.firstName, obj.lastName);
  
// close file
fclose (fd);
  
return 0;
}


Related Solutions

using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
In all walks of life, it is necessary to plan actions before those actions are performed....
In all walks of life, it is necessary to plan actions before those actions are performed. The Audit Standard Guidelines states that the Auditors should adequately plan, control, and record their work: a) What are five of the purposes of planning an audit assignment? b) Describe substantive testing and give examples for the adoption of this procedure for the detailed audit work. c) What key stakeholders should the auditor consult during the planning stage?
In all walks of life, it is necessary to plan actions before those actions are performed....
In all walks of life, it is necessary to plan actions before those actions are performed. The Audit Standard Guidelines states that the Auditors should adequately plan, control, and record their work: a) What are five of the purposes of planning an audit assignment? b) Describesubstantivetestingandgiveexamplesfortheadoptionofthisprocedureforthe detailed audit work. c) What key stakeholders should the auditor consult during the planning stage?
Complete the code(in C) that inserts elements into a list. The list should always be in...
Complete the code(in C) that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void remove_node(struct linked_list* list, int rm_node_value) (the function to make) This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code. This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable number_of_nodes) means the number of nodes that will be removed. When...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code. This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable 'number') means the number of nodes that will be removed. When...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void remove_list(struct linked_list* list) //*This is the function to make and below is the explanation that should be written in given code. "This function removes the list. When the list is removed, all the memory should be released to operating system (OS) so that OS lets other computer programs use this. While deleting the list, every...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void push_Stack (struct linked_list* list, struct linked_node* node) //*This is the function to make and below is the explanation that should be written in given code. This function inserts a node in stack manner. If the type of list is not stack, print the error message “Function push_Stack: The list type is not a stack” The...
For the following table, mark an X for all the actions performed by the listed muscles....
For the following table, mark an X for all the actions performed by the listed muscles. Muscles acting on the neck Fibers Flexion Extension Rotation Lateral flexion Sternocleidomastoid Unilateral Bilateral Scalenes Unilateral Bilateral Splenius Unilateral Bilateral Trapesius Superior Levator scapula Longissimus capitus Semispinalis capitis
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT