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
|
#include <iostream>
#include<fstream>// pour ofstream et ifstream entre autres
#include<sstream>// pour isstringstream
#include <stdlib.h>
#include <string.h>
/********************/
using namespace std;
/********************/
//char chaineCaracteres[500+1]={'\0'}; sert à rien voir string
string const lienFichier("F:\\prog\\carnetDadresses.txt");
string chaineProvisoire;
int numeroFiche = 0;
struct Personne {
char nom[32];
char prenom[32];
char numero[32];
};
void enregistrer(Personne *db, int taille) {
ofstream monFlux(lienFichier.c_str());
cout << endl << "Liste:" << endl;
for (int i=0; i<taille; i++)
monFlux << db[i].nom << " " << db[i].prenom << " " << db[i].numero << endl;
// cout << i+1 << ") " << db[i].nom << " " << db[i].prenom << " " << db[i].numero << endl;
//cout << endl;
cout<<"coucou enregistrement demande"<<endl;
}
void lire(Personne *existant){
numeroFiche=0;
string ligne, mot;
int compteur =1, position=1;
ifstream monFlux(lienFichier.c_str());
while(getline(monFlux,ligne))
{
istringstream iss(ligne);
cout <<ligne<<endl;
while(getline(iss, mot,';'))
{
if (position==1){
existant->nom;
}
else if (position==2){
existant->prenom;
}
else if (position==3){
existant->numero;
}
position++;
if (position==4){position =1;}
}
//existant++;
numeroFiche++;
}
cout<<"lecture de fichier demandee"<<endl;
}
void nouveau_numero(Personne *nouveau) {
cout << "Nouvelle entree dans la base de donnees:" << endl;
cout << "Entrez le nom: ";
cin >> nouveau->nom;
cout << "Entrez le prenom: ";
cin >> nouveau->prenom;
cout << "Entrez le numero: ";
cin >> nouveau->numero;
if (nouveau->nom[0] >= 'a' && nouveau->nom[0] <= 'z') // minuscule
nouveau->nom[0] += 'A' - 'a'; // majuscule
}
void recherche(Personne *db, int taille) {
char search[32];
cout << " Entrez le nom a chercher: ";
cin >> search;
int index = 0;
while (index < taille && strcmp(db[index].nom, search) != 0)
index++;
if (index < taille)
cout << "Le numero de " << search << " est " << db[index].numero << endl;
else
cout << "Le nom '" << search << "' n'existe pas dans la base de donnees." << endl;
}
void liste(Personne *db, int taille) {
cout << endl << "Liste:" << endl;
for (int i=0; i<taille; i++)
cout << i+1 << ") " << db[i].nom << " " << db[i].prenom << " " << db[i].numero << endl;
cout << endl;
}
int main(int argc, char **argv) {
const int max_adresses = 20;
Personne db[max_adresses];
//int numeroFiche = 0;//tranférée en globale
bool fin = false;
char choix;
do {//pour mémoire c'est un do while
cout << "1) Nouveau numero" << endl;
cout << "2) Recherche" << endl;
cout << "3) Liste" << endl;
cout << "4) lister le dossier" << endl;
cout << "0) Quitter" << endl;
cout << "*******************"<< endl;
cout << "e) Enregistrer"<< endl;// on peut mettre 1 seul caractere (lettre ou chiffre ou signe
cout << "l) Lire le fichier"<< endl;// on peut mettre 1 seul caractere (lettre ou chiffre ou signe
cin >> choix;
switch (choix) {
case '1':
if (numeroFiche < max_adresses) {
nouveau_numero(db + numeroFiche);
numeroFiche++;
}
else
cout << "Base de donnees pleine\n";
break;
case '2':
recherche(db, numeroFiche);
break;
case '3':
liste(db, numeroFiche);
break;
case '4':
//system("dir F:/prog/bddCpp/*.* > F:/prog/petiteBddTest/results1.txt");// INCORRECT (pour linux)
system("dir F:\\prog\\bddCpp\\*.* > F:\\prog\\petiteBddTest\\results1.txt");
break;
case '0':
fin = true;
break;
case'e':
enregistrer(db, numeroFiche);
break;
case'l':
lire(db + numeroFiche);
break;
}
} while (!fin);
return 0;
} |
Partager