Nombre de fichiers où appartient un terme
Bonjour,
j'ai un dossier de fichiers textes et je voulais les parcourir mot à mot pour savoir le nombre de fichiers où appartient chaque terme.
le résultat de ce code est erroné, la faute est dans la fonction nbfiles, j'ai pas su le calculer,
svp aidez moi, pleaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaase.
Code:
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
|
#include <dirent.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <map>
using std::string;
using std::ifstream;
bool isValid(const std::string& path)
{
return !path.empty() && path != "." && path != "..";
}
void fillMap(std::map<std::string, int>& map, const std::string& filename)
{ std::ifstream entree(filename.c_str());
std::string s;
while (entree >> s)
++map[s];
}
bool exists(const std::string& filename, const std::string& pattern)
{
std::ifstream entree(filename.c_str());
std::string s;
while (entree >> s)
if (s == pattern)
return true;
return false;
}
void nbfiles(const std::string& filename)
{ std::map<std::string, int> map;
fillMap(map, filename);
int nbFile = 0;
int som = 0;
typedef std::map<std::string, int>::const_iterator iter;
for (iter it = map.begin(); it != map.end(); ++it)
if (exists(filename, it->first))
{++nbFile;
break;
}
for (iter it = map.begin(); it != map.end(); ++it)
std::cout << "Le nombre de fichier contenant le terme " << it->first << " est " << nbFile << std::endl<< std::endl;
}
int main()
{
static const std::string folder = "in";
string s;
DIR* rep = opendir(folder.c_str());
if (rep)
{
struct dirent* lecture;
int nbFile = 0;
int totalFile = 0;
while ((lecture = readdir(rep)))
{
if (!isValid(lecture->d_name))
continue;
nbfiles(folder + "/" + lecture->d_name);
std::cout << std::endl;
++totalFile;
}
closedir(rep);
std::cout << "Le nombre de fichiers est : " << totalFile << std::endl;
}
system ("pause");
return 0;
} |