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
|
#include "mwc.h"
void countlines(std::vector <std::string> infiles, bool verbose) {
const size_t fno = infiles.size(); // Amount of files
std::string fname, line;
int i, total = i = 0, perfile[fno];
std::ifstream f;
for (i = 0; i < int(fno); ++i)
perfile[i] = 0;
i = 0;
while (i < int(fno)) {
fname = infiles.back(); // Current filename is last on vector (LIFO vector)
f.open(fname.c_str());
if (f.is_open()) {
while (!f.eof()) {
std::getline(f, line);
total++; // One more line in total
perfile[i]++; // One more line in file i
}
}
f.close();
infiles.pop_back(); // Remove filename we just used
++i;
}
i = 0;
while (i < int(fno)) {
if (verbose) {
std::cout << "Amount of lines in file #" << i;
std::cout << ": " << perfile[i];
std::cout << "\tFile: " << infiles[i] << "\n";
}
++i;
}
std::cout << "Total amount of lines in all " << fno << " file(s): ";
std::cout << total << std::endl;
}
void countwords(std::vector <std::string> infiles, bool verbose) {
const size_t fno = infiles.size(); // Amount of files
std::string fname, line;
int i, total = i = 0, perfile[fno];
std::ifstream f;
for (i = 0; i < int(fno); ++i)
perfile[i] = 0;
i = 0;
while (i < int(fno)) {
fname = infiles.back(); // Current filename is last on vector (LIFO vector)
f.open(fname.c_str());
if (f.is_open()) {
while (!f.eof()) {
std::getline(f, line);
for (int j = 0; j < int(line.length()); ++j) {
if (line[j] == ' ' || line[j] == '\n') {
total++; // Add another word to total
perfile[i]++; // Counted another word
}
}
}
}
f.close();
infiles.pop_back(); // Remove filename we just used
++i;
}
i = 0;
while (i < int(fno)) {
if (verbose) {
std::cout << "Amount of words in file #" << i;
std::cout << ": " << perfile[i];
std::cout << "\tFile: " << infiles[i] << "\n";
}
++i;
}
std::cout << "Total amount of words in all " << fno << " file(s): ";
std::cout << total << std::endl;
}
void countchars(std::vector <std::string> infiles, bool verbose) {
const size_t fno = infiles.size(); // Amount of files
std::string fname, line;
int i, total = i = 0, perfile[fno];
std::ifstream f;
for (i = 0; i < int(fno); ++i)
perfile[i] = 0;
i = 0;
while (i < int(fno)) {
fname = infiles.back(); // Current filename is last on vector (LIFO vector)
f.open(fname.c_str());
if (f.is_open()) {
while (!f.eof()) {
std::getline(f, line);
total += line.length();
perfile[i] += line.length();
}
}
f.close();
infiles.pop_back(); // Remove filename we just used
++i;
}
i = 0;
while (i < int(fno)) {
if (verbose) {
std::cout << "Amount of chars in file #" << i;
std::cout << ": " << perfile[i];
std::cout << "\tFile: " << infiles[i] << "\n";
}
++i;
}
std::cout << "Total amount of chars in all " << fno << " file(s): ";
std::cout << total << std::endl;
}
|