Hello I am pretty new in regards to fstream and file manipulation. So I have a hw problem where I am reading a file with a list of <first name> <M or F> <frequency of name> and my job is to read line by line taking each bit of info and passing into an object. Then pass that object into a vector. I am having the problem of how do each piece of information from the file and save into the according variable within the object.
for example
taking the first name of the person and passing into a string variable called name.
Another example is taking the frequency and storing it into an int.
I would like some help spear heading my project because I generally don't know how to pull indivudal info and store it. I know how to store the entire file or line into a single vector but not multiple data types.
It can be as simple as the following, or it can be more object-oriented if you want (making data private, defining overloads for operator<< and operator>>, etc.)
it has to be a bit more object-oriented ish.
this is the code I have been working with/ trying to work; however, it keeps iterating through the begin two while loops
Ok so I am a bit confused how does doing something like (ioFile >> tempName >> tempGender >> tempFreq) allow me to extract each data type that I need becaue when I throw it through a debugger it just ends without separating the first line of code into. instread it assigns the first two line of code two the two string varibles
something like this
tempName = <name of person>
tempGender = <M or F>
tempFreq = 48493
My Bad I am tired, I meant to say is this. When I run this line of code (ioFile >> tempName >> tempGender >> tempFreq) it does not separate what is being read from the file which is some thing like this "John, M, 487267". so I am just confused in general on how to separate something like this "John, M, 487267" in the following:
string tempName = John
char tempGender = M
int tempFreq = 487267
Thank you for helping me my teacher with this whole online learn he does not explain things well.
I am also trying a different method of separating the line of code, does something like this work better?
1 2 3 4 5 6 7 8 9
while (!ioFile.eof()) {
getline(ioFile, read); //reads line by line
test.push_back(read); //pushes the line into a string vector
baby bf; //creates an object for the info to be stored
for (int i = 0; i < test.size(); i++) { //runs through entire line
if (test[i] == "F") { //checking if the char at this position is Female or male
bf.setGender(test[i]); // suppose to set a char to object
}
}
So this is what i have in my code right now. so it separated the name but leaves
"F, 84758" in the gender variable and skips over freq. since gender is a char could I do something like freq = atoi(gender) and then set the object's freq. However, i don't know how to get rid of everything past the F in the gender varible
You may wish to further re-factor the design and implementation of this code for several reasons – the main ones being code re-usability and conformance to the single responsibility principle (refer to DRY and SOLID principles).
Your task is to parse a text file containing data (stored in a pre-defined format) in order to construct an in-memory representation of this data (for further processing).
You could look at implementing further classes related to the parsing of the file to construct the in-memory representation of this data. This could be achieved simply by creating a PersonTextFileParser class.
A more advanced approach would be to implement an abstract base class (e.g., PersonFileParserI) to provide a common interface for classes which allow for the parsing of this data from files of different formats (e.g., PersonTextFileParser, PersonXmlFileParser, etc).
The same approach could then be applied for classes which generate an output file from the in-memory representation of the data.
But this requires some understanding of class inheritance, abstract interface classes, etc.