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?
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...
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
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
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct QueueType *...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT