The person.txt file contains data in a table storing Name, Sex, Address and phone number. The amount of lines of data is not known,data reading is terminated by a line containing "end" only. Declare a struct "Person" to store the information on each person and write a function, readPersoninfo, which accepts a one-dimensional array of Person structs as a parameter and reads the data from person.txt and stores it in the array. The function should return the amount of persons that was read from the file.
1
Expert's answer
2018-04-10T02:22:11-0400
The specific algorithm for reading the data will depend on the format of the input file. Suppose that the file "person.txt" has csv format, i.e. all fields are separated by commas and there are no spaces between the data and commas:
person.txt John Rich,M,Triana 188 Huntsville 35806 AL,999999999 end
string line; // line buffer int count = 0; // line counter
// Read input stream line by line // until read "end" line while (getline(in, line) && line != END) { // Create string stream from line // (it is more convenient to work with stream than with a string) stringstream str(line);
// Read Name from string stream up to FIELD_SEPARATOR getline(str, person[count].Name, FIELD_SEPARATOR); // Read Sex from string stream up to FIELD_SEPARATOR getline(str, person[count].Sex, FIELD_SEPARATOR); // Read Address from string stream up to FIELD_SEPARATOR getline(str, person[count].Address, FIELD_SEPARATOR); // Read Phone from string stream up to end of stream getline(str, person[count].Phone);
++count; // increase line counter
if (count == MAX_PERSON) // if reached max array size return count; }
// return line count return count; }
int main() { // Create array of Person Person person[MAX_PERSON];
// Read person info int count = ReadPersonInfo(person);
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment