1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/**
* @author blongho.
*
* @brief Given two files, read the contents in the file,
* separate the males from the females and save the females
* in a different file and males in another file.
* At the end of the program, tell user what the files contained.
*
* Constraints: Use dynamic arrays
* Origin: http://www.cplusplus.com/forum/beginner/242250/#msg1075756
*
* @since 2018-09-10
*/
#include <iostream>
#include <fstream>
#include <string>
/** datafile structure */
struct DataFile
{
int numOfRecords;
std::string firstName;
std::string lastName;
char sex;
void setNumOfRecords(std::ifstream &ifs, const std::string &filename) {
ifs.open(filename, std::ios::in); // open the
if (ifs) {
ifs >> numOfRecords;
}
else {
std::cout << "Could not open file {" << filename << "}" << std::endl;
}
}
};
/**
* After you have the number of records, read the rest values and put into a
* DataFile object
*
* @param [in,out] ifs The ifstream object.
* @param [in,out] datafile [in,out] array of DataFile objects.
* @param records The number of records in the file.
* @param [in,out] males The males the number of males in the file(to be obtained).
* @param [in,out] females The females the number of females int he file (to be obtained).
*/
void readRecords(std::ifstream &ifs, DataFile * &datafile, int records, int &males, int &females) {
int ind{};
while (ind < records) {
DataFile tmp;
ifs >> tmp.firstName >> tmp.lastName >> tmp.sex;
datafile[ind++] = tmp;
if (tmp.sex == 'F' || tmp.sex == 'f') {
females++;
}
else {
males++;
}
}
}
/**
* Process an array of DataFile objects and save males and females into
* different files
*
* @param [in,out] female The file to save the female records
* @param [in,out] male The file to save the male records
* @param [in,out] datafile [in,out] The dataFile array .
* @param records The records the number of records in the DataFile array.
*/
void saveRecord(std::ofstream &female, std::ofstream&male, DataFile * &datafile, int records) {
for (int i = 0; i < records; i++) {
if (datafile[i].sex == 'F' || datafile[i].sex == 'f') {
female << datafile[i].firstName << ' ' << datafile[i].lastName << '\n';
}
else {
male << datafile[i].firstName << ' ' << datafile[i].lastName << '\n';
}
}
}
// Say what a file contains
void decribeContent(const std::string & filename, int males, int females, std::ostream &os = std::cout) {
os << filename << " contained " << females << " female names and "
<< males << " male names\n";
}
int main()
{
DataFile dataFile1, dataFile2;
std::ifstream ifs, ifs2;
std::string infile1{ "datafile1.txt" };
std::string infile2{ "datafile2.txt" };
std::string outfile1{ "males.txt" };
std::string outfile2{ "females.txt" };
dataFile1.setNumOfRecords(ifs, infile1);
dataFile2.setNumOfRecords(ifs2, infile2);
int numOfRecordsInDataFile1 = dataFile1.numOfRecords;
int numOfRecordsInDataFile2 = dataFile2.numOfRecords;
DataFile * records1 = new DataFile[numOfRecordsInDataFile1];
DataFile * records2 = new DataFile[numOfRecordsInDataFile2];
std::ofstream maleFile{ outfile1 };
std::ofstream femaleFile{ outfile2 };
int males1{}, females1{}, // males and females from datafile1
males2{}, females2{}; // males and females from datafile2
if (ifs) {
readRecords(ifs, records1, numOfRecordsInDataFile1, males1, females1);
}
else { std::cout << " Could not open the file '" << infile1 << "'" << std::endl; }
if (ifs2) {
readRecords(ifs2, records2, numOfRecordsInDataFile2, males2, females2);
}
else {
std::cout << " Could not open the file '" << infile2 << "'" << std::endl;
}
// Start saving files
femaleFile << females1 + females2 << " Females\n";
maleFile << males1 + males2 << " Males\n";
// extract data from first record and save to file
saveRecord(femaleFile, maleFile, records1, numOfRecordsInDataFile1);
saveRecord(femaleFile, maleFile, records2, numOfRecordsInDataFile2);
// describe records
decribeContent(infile1, males1, females1);
decribeContent(infile2, males2, females2);
// free resources
ifs.close();
ifs2.close();
maleFile.close();
femaleFile.close();
delete[]records1;
delete[]records2;
return 0;
}
|