Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #define FILENAME "occorenza2.dat"
  5. using namespace std;
  6.  
  7. class dipendente
  8. {
  9.  
  10. public:
  11.     char nome[20];
  12.     int eta;
  13.  
  14.     dipendente(const char *nome, int eta);
  15. };
  16.  
  17.  
  18.  
  19. dipendente::dipendente(const char *s = "", int i = 0)
  20. {
  21.     strncpy(nome, s, sizeof(nome));
  22.     eta = i;
  23. }
  24. int main()
  25. {
  26.     unsigned int n_righe;
  27.     int eta;
  28.     string nome;
  29.     char c;
  30.  
  31.    fstream fout(FILENAME, ios::out | ios::binary);
  32.     if(!fout)
  33.     {
  34.         cout << "Error open write file" << endl;
  35.         return 1;
  36.     }
  37.  
  38.     do
  39.     {
  40.         cout << "Inserisci numero di oggetti 1 e 20: ";
  41.         cin >> n_righe;
  42.  
  43.  
  44.  
  45.     } while ((n_righe < 1) || (n_righe > 20));
  46.    std::getline(cin, nome);
  47.    cin.clear();
  48.  
  49.     for (unsigned int i=1; i<=n_righe; ++i)
  50.     {
  51.         cout << "Inserisci nome: ";
  52.         std::getline(cin, nome);
  53.         cin.clear();
  54.         cout << "Inserisci eta: ";
  55.         cin>>eta;
  56.         dipendente dip1(nome.c_str(), eta);
  57.         fout.write((char *)&dip1, sizeof(dip1));
  58.     }
  59.     fout<<flush;
  60.     fout.close();
  61.     cout << "Inserisci carattere da ricercare: ";
  62.     cin >> c;
  63.  
  64.     cin.clear();
  65.     int x,i,countc=0;
  66.     fstream fin(FILENAME, ios::in | ios::binary);
  67.     if(!fin)
  68.     {
  69.         cout << "Error open read file" << endl;
  70.         return 1;
  71.     }
  72.     while (!fin.eof())
  73.     {
  74.     dipendente dip2;
  75.  
  76.     fin.read((char *)&dip2, sizeof (dip2));
  77.  
  78.         x=strlen(dip2.nome);
  79.         for (i=0; i<x; i++)
  80.         {
  81.             if (strcmp(dip2.nome,c)==0)
  82.                 countc++;
  83.         }
  84.  
  85.     }
  86.     cout<<countc;
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement