In: Computer Science
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.
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;
}