75.4k views
4 votes
The readLikes function will fill an array of User objects and their likes for Posts. Each line in the file used to fill the array consists of a username followed by a sequence of likes; as such, each line corresponds to a single User object. Note this is a standalone function rather than a class member function.

This function should:
Accept four parameters in this order:
string file_name: the name of the file to be read
User users]: array of User objects
int num_users_stored: the number of users currently stored in the users array. You can always assume this is the correct number of actual elements
in the array
oint users_arr_size: capacity of the users array
int max_posts: maximum number of likes stored on each line in the file; i.e., number of posts available to the user. Note the number of likes on a given line can be less than max posts. If fewer than max posts likes are found on a given line, ensure the User data member num posts is set to the actual number of likes found. Assume max_posts will be less than or equal to the maximum size of the likes array (50)
Use ifstream and getline to read data from the file, creating a new User object for each line, and placing the object into the users array.
• Hint: You can use the split function from Homework 5 with a comma (,) as the delimiter.
•Hint: You can use stoi to convert each like value (a string, as read from the text file) into an integer value.
Empty lines should not be added to the arrays.
• The function behavior should match the following scenarios:
Before opening the file, if num_users_stored is equal to users_arr_size, return -2.
If the file is not opened successfully, return -1.
The priority of the return code -2 is higher than -1, Le., in cases when num_users_stored is equal to users_arr_size and the file cannot be opened, the function should return -2.
While num_users_stored is smaller than users_arr_size, keep the existing elements in users, then read data from the file and add (append) the data to
the array. The number of users stored in the array cannot exceed the size of the array. Return the total number of users in the system, as an integer.

User Ben Marini
by
7.8k points

1 Answer

1 vote

Final Answer:

The `readLikes` function, a standalone function, reads data from a specified file, creating User objects for each line, and populates the provided array of User objects with their respective likes for posts.

Step-by-step explanation:

The `readLikes` function begins by checking if the array is already full (`num_users_stored` equals `users_arr_size`). If so, it returns -2, indicating that no additional users can be added. Next, it attempts to open the specified file using `ifstream` and checks if the file opens successfully. If the file cannot be opened, it returns -1, signaling an error.

Assuming the file opens successfully, the function proceeds to read data from the file using `getline`. For each line, it creates a new User object, extracts the username and sequence of likes using the `split` function, and populates the User object accordingly. The function then appends this User object to the `users` array. This process continues until the array is full or the file is fully processed.

The function returns the total number of users in the system, considering both the existing users in the array (`num_users_stored`) and the newly added users. The implementation ensures the array doesn't exceed its capacity. This modular function allows for efficient reading and incorporation of user data from the file into the existing array.

User Apaatsio
by
8.1k points