Question

In: Computer Science

Write the following two functions maintaining a database of entries of type person in a file....

Write the following two functions maintaining a database of entries of type person in a file. (see .h file for the definition of point) person* find_person(const char* file_name, int ID) The function gets a name of a file and searches the file for a person with the given ID. It returns a pointer to the struct person (on heap) with the name and ID of the person. If the file does not exist or if the person with the required ID is not in the file, the function returns the NULL pointer. The file should not be changed by this function. int add_person(const char* file_name, person p) The function gets a name of a file, and a person p. If the person with the same ID is already in the file, the function does nothing. If the person is not in the file, the function adds p to the file. If the file does not exist, a new file is created and the person is added to the file. The function returns 0 if the person is added to the file. The function returns 1 if the person has already been in the file. The function returns -1 if there was an error (e.g. error opening a file)

Solutions

Expert Solution

source code :

person find_person(const char file_name, int ID)
{
// Declare a pointer fp that points to type FILE
FILE *fp;
// Open the file
fp = fopen(file_name, "r");

// If File doesn't exist or error in opening file
if (fp == NULL)
{
return NULL;
}

//Variables to store person name and id
char person_name[100];
int person_ID;

// Iterate through the file until EOF(End of File)
while (fscanf(fp, "%d %s", &person_ID, person_name) != EOF)
{
// If person_ID matches the required ID return the pointer to the person type
if (person_ID == ID)
{
fclose(fp);
person *person_pointer = malloc(sizeof(person));
person_pointer->id = person_ID;
person_pointer->name = person_name;
return person_pointer;
}
}
// Close the temporary file
fclose(fp);
return NULL;
}

/*
add_person
If person with ID is in file [1]
If the person is not in the file [add p to file] [0]
If the file does not exist [create new file] [add p to file] [0]
If error opening file [-1]
*/

int add_person(const char *file_name, person p)
{
// Declare a pointer fp that points to type FILE
FILE *fp;
// Open the file
fp = fopen(file_name, "a");

// If file doesn't exist
if (fp == NULL)
{
// there was error creating a new file or accessing old file
fclose(fp);
return -1;
}
// Find the person using the previous find_person function
person *person_pointer = find_person(file_name, p.id);
if (person_pointer != NULL)
{
fclose(fp);
return 1;
}

// add the person in the file.
fprintf(fp, "%d %s\n", p.id, p.name);
// Close the file
fclose(fp);
// Return 0 as a default case i.e. success
return 0;
}


Related Solutions

(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that person likes Express the following queries in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that a person likes Express the following queries...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in...
Consider the following database schema: LIKE(person, sport), PRACTICE(person, sport), where person and sport are keys in both tables. The table LIKE gives the sports a person likes, the table PRACTICE gives the sports a person practices. We assume that a person likes at least one sport and practices at least one sport. We assume also that a person does not like a sport if the sport is not listed among the sports that person likes. Express the following queries in...
Using the following accounts, write the TWO entries needed per transaction. Only two entries per each....
Using the following accounts, write the TWO entries needed per transaction. Only two entries per each. Accounts: Accounts payable, payroll payable, accumulated depreciation, cash, cost of goods sold, prepaid insurance, Manufactory overhead, Materials inventory, Work in process Inventory, Finished goods inventory a. Indirect materials were purchased on credit. b. Direct materials were used into production. c. The cost of the salary involved in production was accumulated. d. The insurance policy expired and the adjustment was made. and. Indirect manufacturing costs...
Suppose there are two types of person, Type M and Type F. Suppose a researcher named...
Suppose there are two types of person, Type M and Type F. Suppose a researcher named Smith is interested in analyzing possible pay discrimination between the two types of person. With a representative sample of observations from a reliable survey, suppose Smith estimates that monthly earnings (W) are related to schooling (S) as follows: WM = 895 + 210SM, WF = 905 + 180SF,where the subscripts “M” and “F” stand, respectively, for “Type M” and “Type F”. Suppose SMexhibits a...
Give two examples of how each tissue type assists in maintaining homeostas - Integumentary - Muscular...
Give two examples of how each tissue type assists in maintaining homeostas - Integumentary - Muscular -bone -Nervous
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write...
Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write a function that consists of a set of loops that run through an array and count the number of ones in it. Do the same thing using the where() function (use info(where) to find out how to use it) (in python)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT