Question: The readPosts function will fill an array of Post objects. Note this is a standalone function rather than a class member function. This function should:

The readPosts function will fill an array of Post objects. Note this is a standalone function rather than a class member function.

This function should:

Have four parameters in this order: string file_name: the name of the file to be read. Post posts[]: array of Post objects. int num_posts_stored: the number of posts currently stored in the array. You should assume this is the correct number of actual elements in the array. int posts_arr_size: capacity of the posts array. The default value for this is 50. Use ifstream and getline to read data from the file, creating a new Post object for each line, and placing the object into the posts array. Empty lines should not be added to the array. Assume that Post content can have spaces in them. Hint: You can use the split() function from Homework 5 and the readPosts() function from Homework 6. The function behavior should match the following scenarios: If num_posts_stored is equal to posts_arr_size, return -2. If the file is not opened successfully, return -1. The priority of the return code -2 is higher than -1, i.e., in cases when num_posts_stored is equal to posts_arr_size and the file cannot be opened, the function should return -2. While num_posts_stored is smaller than posts_arr_size, keep the existing elements in the posts array, then read data from the file and add (append) the data to the array. The number of posts stored in the array cannot exceed the size of the array. Returns the total number of posts in the system, as an integer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!