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
| #include <iostream>
#include <string>
#include <fstream>
using namespace std;
const string fichData = "D:\\Atelier3A6\\fichierIndexé\\d";
const string fichIndex = "D:\\Atelier3A6\\fichierIndexé\\index";
void rechercheNonIndexe(string strCrit) {
ifstream ifEntree(fichData,ios::in);
string strCourant;
while(ifEntree.good()) {
getline(ifEntree, strCourant);
if (!strCourant.find(strCrit)) {
int pos = strCourant.find(',');
string fourCode = strCourant.substr(0,pos);
string fourNom = strCourant.substr(pos+1);
cout << "séquentiel: " << fourCode << " " << fourNom << endl;
break;
}
}
ifEntree.close();
}
void indexation() {
ifstream ifEntree(fichData,ios::in);
ofstream ofIndex(fichIndex,ios::out);
string strCourant;
int posTotale = 0;
while (ifEntree.good()) {
getline(ifEntree, strCourant);
if (!strCourant.find("four-")) {
int pos = strCourant.find(',');
string fourCode = strCourant.substr(0,pos);
string fourNom = strCourant.substr(pos+1);
ofIndex << fourCode << ": " << posTotale << endl;
cout << ".";
}
posTotale += strCourant.length();
posTotale += 2; // Ajouter le \n qui compte pour 2
}
cout << "indexation terminée" << endl;
ifEntree.close();
ofIndex.close();
}
void rechercheIndexe(string strCrit) {
ifstream ifIndex(fichIndex,ios::in);
string strCourant;
int positionTotale = 0;
while(ifIndex.good()) {
getline(ifIndex, strCourant);
if (!strCourant.find(strCrit)) {
int pos = strCourant.find(':');
string fourCode = strCourant.substr(0,pos);
string strPositionData = strCourant.substr(pos+1);
ifstream ifEntree(fichData,ios::in);
int iPositionData = stoi(strPositionData);
ifEntree.seekg(iPositionData);
string strLigneData;
getline(ifEntree, strLigneData);
pos = strLigneData.find(',');
fourCode = strLigneData.substr(0,pos);
string fourNom = strLigneData.substr(pos+1);
cout << "indexé: " << fourCode << " " << fourNom << endl;
ifEntree.close();
break;
}
}
ifIndex.close();
}
void main() {
setlocale(LC_ALL,"");
for (int i =0;i<20;i++)
rechercheNonIndexe("four-3");
indexation();
for (int i =0;i<20;i++)
rechercheIndexe("four-3");
} |
Partager