Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #define FILENAME "occorenza2.dat"
- using namespace std;
- class dipendente
- {
- public:
- char nome[20];
- int eta;
- dipendente(const char *nome, int eta);
- };
- dipendente::dipendente(const char *s = "", int i = 0)
- {
- strncpy(nome, s, sizeof(nome));
- eta = i;
- }
- int main()
- {
- unsigned int n_righe;
- int eta;
- string nome;
- char c;
- fstream fout(FILENAME, ios::out | ios::binary);
- if(!fout)
- {
- cout << "Error open write file" << endl;
- return 1;
- }
- do
- {
- cout << "Inserisci numero di oggetti 1 e 20: ";
- cin >> n_righe;
- } while ((n_righe < 1) || (n_righe > 20));
- std::getline(cin, nome);
- cin.clear();
- for (unsigned int i=1; i<=n_righe; ++i)
- {
- cout << "Inserisci nome: ";
- std::getline(cin, nome);
- cin.clear();
- cout << "Inserisci eta: ";
- cin>>eta;
- dipendente dip1(nome.c_str(), eta);
- fout.write((char *)&dip1, sizeof(dip1));
- }
- fout<<flush;
- fout.close();
- cout << "Inserisci carattere da ricercare: ";
- cin >> c;
- cin.clear();
- int x,i,countc=0;
- fstream fin(FILENAME, ios::in | ios::binary);
- if(!fin)
- {
- cout << "Error open read file" << endl;
- return 1;
- }
- while (!fin.eof())
- {
- dipendente dip2;
- fin.read((char *)&dip2, sizeof (dip2));
- x=strlen(dip2.nome);
- for (i=0; i<x; i++)
- {
- if (strcmp(dip2.nome,c)==0)
- countc++;
- }
- }
- cout<<countc;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement