Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Classe per Gestire File.txt   v.1.0 - HeaderToti.h

HeaderToti.h

Caricato da: Totipianurese
Scarica il programma completo

  1. #ifndef __FILE_H__
  2. #define __FILE_H__
  3.  
  4. # include <iostream>
  5. # include <stdio.h>
  6. # include <fstream>
  7. # include <string>
  8. # include <sstream>
  9. # include <windows.h>
  10. # include <math.h>
  11.  
  12. using namespace std;
  13.  
  14. bool ViaLibera;
  15. int ID;
  16. string punt;
  17. string Svet[250];
  18. int Ppunt[250];
  19. string Clog[250];
  20.  
  21. class File
  22. {
  23.  
  24.  public:
  25.         void ApriFileInScrittura(string NomeFile)
  26.         {
  27.                 ofstream write(NomeFile.c_str(), ios::app);
  28.         }  
  29. /************************************FINE**************************************/    
  30.     void GeneraID(string NomeFile)
  31.     {
  32.      ifstream trovaID("ID.txt", ios::in); //ID.txt file contentente il numero                                     // dell'ultimo ID inserito
  33.      if (trovaID)
  34.               {
  35.                trovaID >> ID;
  36.                ID=ID+1;
  37.                trovaID.close();
  38.               }
  39.               else
  40.               ID=1;
  41.  
  42. ofstream write(NomeFile.c_str(),ios::app);                
  43. ////// SCRIVI  ID////////////
  44.      stringstream s;      //  
  45.      string ss;          //          
  46.      s << ID ;          //
  47.      ss = s.str();     // /* Dimesione dell' ID = 5
  48.      ss.resize(5);    //  dato che ss.resize(5); */
  49.      write <<ss<<";";//
  50. //////////////////////  
  51.      write <<"L;";   /* Imposta FLAG per la CANCELLAZIONE LOGICA*/
  52.      write.close();
  53.  
  54.   // Aggiorna L'ID che viene scritto sul file ID.txt
  55.      ofstream aggID("ID.txt", ios::out);  
  56.      aggID << ID;    
  57.      aggID.close();                    
  58.     }
  59. /************************************FINE**************************************/    
  60.     void ScriviCampo(string NomeFile, string Campo, int DimCampo)
  61.     {
  62.      ofstream write(NomeFile.c_str(),ios::app);
  63.      Campo.resize(DimCampo);
  64.      write << Campo;
  65.      write.close();    
  66.     }
  67. /************************************FINE**************************************/    
  68.     void ScriviUltimoCampo(string NomeFile, string Campo, int DimCampo)
  69.     {
  70.      ofstream write(NomeFile.c_str(),ios::app);
  71.      Campo.resize(DimCampo);
  72.      write << Campo << endl;
  73.      write.close();    
  74.     }
  75. /************************************FINE**************************************/      
  76.     void ScriviCampoChiavePrimario(string NomeFile, string NomeFileChiave ,string Campo, int DimCampo)
  77.     {      
  78.      string controllo;  
  79.      ifstream leggi(NomeFileChiave.c_str(), ios::in);
  80.      if (leggi)
  81.                {
  82.                  while(!leggi.eof())  
  83.                                   {
  84.                                    getline(leggi,controllo,';');                        
  85.                                    getline(leggi,controllo,'\0');
  86.                                    getline(leggi,punt,'\n');
  87.                                    if (controllo==Campo)
  88.                                     {
  89.                                       cout << "Questo e' un campo chiave gia assegnato" << endl;
  90.                                       cout << "Scegline un'altro" << endl;
  91.                                       ViaLibera=true;
  92.                                     }
  93.                                   }
  94.                }
  95.      if (ViaLibera==false && !leggi)
  96.                 {
  97.                  int puntatoreID=ID-1;        
  98.                  ofstream write(NomeFileChiave.c_str(),ios::app);
  99.                  Campo.resize(DimCampo);
  100.                  write << " ;" << Campo;
  101.                  string idf=Dimensiona(puntatoreID,3);
  102.                  write << ";"<< idf << endl;
  103.                  write.close();
  104.                  ofstream scrivi(NomeFile.c_str(),ios::app);
  105.                  scrivi << Campo;
  106.                  scrivi.close();  
  107.                 }
  108.     }
  109. /************************************FINE**************************************/
  110.     void ScriviChiaveSecond(string NomeFile, string NomeFileChiaveSecondario, string Campo, int DimCampo)
  111.     {
  112.      ofstream scrivi(NomeFileChiaveSecondario.c_str(),ios::app);
  113.      Campo.resize(DimCampo);
  114.      scrivi << " ;" << Campo;
  115.      string idf=Dimensiona(ID-1,3);
  116.      scrivi << ";" << idf << endl;
  117.      scrivi.close();          
  118.      ofstream write(NomeFile.c_str(), ios::app);
  119.      write << Campo;
  120.      write.close();  
  121.     }
  122. /************************************FINE**************************************/  
  123.     void ScriviUltimoCampoChiaveSecond(string NomeFile, string N_F_UltimoCampo, string Campo, int DimCampo)
  124.     {
  125.      ofstream write(N_F_UltimoCampo.c_str(),ios::app);
  126.      Campo.resize(DimCampo);
  127.      write << " ;" << Campo;
  128.      string idf=Dimensiona(ID-1,3);
  129.      write << ";" << idf << endl;
  130.      write.close();  
  131.      ofstream scrivi(NomeFile.c_str(),ios::app);
  132.      scrivi << Campo << endl;
  133.      scrivi.close();
  134.     }
  135. /************************************FINE**************************************/
  136.     void CancellazioneLogica(string NomeFile ,int id)
  137.     {
  138.     bool trova=false;
  139.     string IDcanc,restorecord;  
  140.     string ID=Dimensiona(id,5);
  141.     int k=0;
  142.     int q=GrandezzaRecord(NomeFile.c_str());
  143.    
  144.     fstream r_w(NomeFile.c_str(), ios::in | ios::out);
  145.     ID.resize(5);
  146.     while(!r_w.eof())
  147.     {                
  148.      getline(r_w,IDcanc,';');
  149.      if (ID==IDcanc)
  150.                    {
  151.                     trova=true;
  152.                     fstream write(NomeFile.c_str(), ios::in | ios::out);
  153.                     write.seekp((k*q)+6,ios_base::beg);
  154.                     write <<"X";
  155.                     write.close();
  156.                     cout << "Record Cancellato!."<< endl;
  157.                    }
  158.      getline(r_w,restorecord,'\n');  
  159.      k++;  
  160.     }
  161.     if (trova==false)
  162.                      cout << "ID da cancellare non trovato nel file" << endl;                
  163.     }  
  164. /************************************FINE**************************************/
  165. void CancellazioneLogicaChiave(string NomeFileChiave, int id , int DimChiave)
  166.  {
  167.   int x= GrandezzaRecord(NomeFileChiave.c_str());
  168.   int rig=0,puntatore,riga;
  169.   bool trov=false;
  170.   fstream r_w(NomeFileChiave.c_str(),ios::in | ios::out);
  171.   if (r_w)
  172.      {
  173.       while(!r_w.eof())
  174.                        {    
  175.                         r_w.seekp((x*rig)+DimChiave+3,ios_base::beg);
  176.                         r_w >> puntatore;
  177.                         puntatore++;
  178.                         if (puntatore==id)
  179.                                           {
  180.                                            riga=rig;
  181.                                            trov=true;
  182.                                            }                  
  183.                         rig++;
  184.                        }
  185.       r_w.close();                
  186.       if (trov==true)
  187.          {
  188.           fstream scrivi(NomeFileChiave.c_str(),ios::in | ios::out);          
  189.           scrivi.seekp((x*riga),ios_base::beg);
  190.           scrivi << "X";
  191.           scrivi.close();
  192.          }  
  193.          else
  194.          cout << "Id del file da cancellare non presente!" << endl;              
  195.      }
  196.      else
  197.      cout << "Impossibile aprire il file "<<NomeFileChiave.c_str()<< endl;
  198.  
  199.  }
  200. /************************************FINE**************************************/
  201.     void StampaFile(string NomeFile)
  202.     {
  203.      system("CLS");
  204.      string Flag,Riga,IDRiga;
  205.      int k=0;  
  206.      int q=GrandezzaRecord(NomeFile.c_str());
  207.      ifstream read(NomeFile.c_str(),ios::in | ios::out);
  208.      if (read)
  209.               {
  210.                while(!read.eof())
  211.                 {
  212.                  getline(read,IDRiga,';');
  213.                  IDRiga.resize(5);              
  214.                  //ad.seekg(k*q+6,ios_base::beg);
  215.                  getline(read,Flag,';');
  216.                  getline(read,Riga,'\n');
  217.                  if (Flag=="L")
  218.                               {
  219.                                cout << IDRiga;
  220.                                cout << Riga << endl;
  221.                               }
  222.                  Flag=".";
  223.                  k++;                    
  224.                 }
  225.               }
  226.     }
  227. /************************************FINE**************************************/
  228.     void SequencialSearch(string NomeFilePrincipale, string NomeFileChiave, string Campo)
  229.     {
  230.      string confronto,Riga,Riga2,resto,ctr;  
  231.      int ID,puntID;
  232.      int x=GrandezzaRecord("PROVA.txt");
  233.      bool trova=false;
  234.      system("CLS");
  235.      fstream read(NomeFileChiave.c_str(),ios::in | ios::out);
  236.      if (read)
  237.               {
  238.                while(!read.eof())
  239.                   {
  240.                    getline(read,ctr,';');
  241.                    getline(read,confronto,'\0');
  242.                    getline(read,resto,';');
  243.                    read >> puntID;
  244.                    getline(read,resto,'\n');
  245.                    if (confronto==Campo)
  246.                                         {
  247.                                          if (ctr!="X")
  248.                                           {                                                    
  249.                                            trova=true;
  250.                                            ID=puntID;
  251.                                            fstream leggi(NomeFilePrincipale.c_str(),ios::in | ios::out);
  252.                                            leggi.seekp(x*ID, ios_base::beg);
  253.                                            getline(leggi,Riga,';');
  254.                                            leggi.seekp((x*ID)+8,ios_base::beg);
  255.                                            getline(leggi,Riga2,'\n');
  256.                                            cout << Riga << Riga2 << endl;
  257.                                            leggi.close();
  258.                                           }
  259.                                         }  
  260.                    confronto="";                                      
  261.                   }
  262.                read.close();
  263.               }
  264.               else
  265.               cout << "File :"<<NomeFileChiave.c_str()<<" NON TROVATO!" << endl;
  266.              
  267.      if(trova==false)
  268.                     cout << "Campo da ricercare non presente nel File\n" << endl;                      
  269.     }
  270. /************************************FINE**************************************/
  271.    void BinarySearch(string NomeFile, string NomeFileChiave , string Campo, int DimChiave)
  272.    {
  273.    // Da utilizzare per trovare un campo UNIVOCO
  274.     system("CLS");  
  275.     string ricerca,continuo,ctr,stringa,stringa1;
  276.     bool esci=true,trovat=false;
  277.     OrdinaFileChiave(NomeFileChiave.c_str(), DimChiave);
  278.     int x = FileSize(NomeFileChiave.c_str());
  279.     int gr = GrandezzaRecord(NomeFileChiave.c_str());
  280.     int Rinf,Rsup,punta,punt,i,puntatore,puntabyte;
  281.     Rinf = 0;
  282.     i = 0;
  283.     Rsup = x/gr;
  284.     int N_acces = CalcLog(Rsup);
  285.     fstream read(NomeFileChiave.c_str(), ios::in | ios::out);
  286.     do
  287.     {
  288.      if (read)
  289.      {
  290.       punta=(Rinf+Rsup) / 2;
  291.       puntabyte= gr*punta;
  292.       read.seekp(puntabyte,ios_base::beg);
  293.       getline(read,ctr,';');
  294.       getline(read,ricerca,'\0');
  295.       getline(read,continuo,';');
  296.       read >> punt;  
  297.       if (Campo>ricerca)        
  298.                       Rinf = punta;
  299.                       else
  300.                       if (Campo<ricerca)
  301.                                         Rsup=punta;                      
  302.                       else
  303.                       {
  304.                       if (ctr!="X")
  305.                          {
  306.                          esci=false;
  307.                          trovat=true;
  308.                          puntatore=punt;
  309.                          }
  310.                       }
  311.       i++;  
  312.       if (i>N_acces)
  313.                   esci=false;            
  314.      }  
  315.     }
  316.     while(esci);
  317.    
  318.     if (trovat==true)  
  319.        {
  320.         int gr1 = GrandezzaRecord(NomeFile.c_str());
  321.         gr1=gr1*puntatore;            
  322.         fstream leggi(NomeFile.c_str(),ios::in | ios::out);
  323.         leggi.seekp(gr1, ios_base::beg);
  324.         getline(leggi,stringa,';');
  325.         leggi.seekp((gr1)+8,ios_base::beg);
  326.         getline(leggi,stringa1,'\n');
  327.         cout << stringa << stringa1 << endl;        
  328.        }  
  329.        else
  330.        cout << "Campo da ricercare non TROVATO!. " << endl;  
  331.    }
  332. /************************************FINE**************************************/
  333.     void ModificaCampo(string NomeFile, int id,int pos, int DimCampo)
  334.     {      // Modifica di un campo NORMALE
  335.      string Modificato;
  336.      char y = TrovaFlag(NomeFile.c_str(),id);
  337.      if (y=='L')
  338.         {
  339.          cout << "Inserisci il campo modificato: ";
  340.          cin.sync();
  341.          getline(cin,Modificato);
  342.          Modificato.resize(DimCampo);
  343.          fstream write(NomeFile.c_str(),ios::in | ios::out);
  344.          int x=GrandezzaRecord(NomeFile.c_str());
  345.          id=id-1;
  346.          write.seekp((id*x)+pos,ios_base::beg);
  347.          write << Modificato;
  348.          write.close();
  349.         }
  350.         else
  351.         {
  352.          system("CLS");  
  353.          cout << "Impossibile modificare!" << endl;
  354.          cout << "Il record e' stato cancellato logicamente" << endl<< endl;
  355.          cout << "Premi INVIO per continuare" << endl;
  356.          getchar();
  357.          getchar();
  358.         }
  359.     }
  360. /************************************FINE**************************************/
  361.     void AggiungiQuantita(string NomeFile, float agg, int id, int pos, int DimCampo)
  362.     {
  363.      float quant;
  364.      string campo;
  365.      id = id - 1;
  366.      int x=GrandezzaRecord(NomeFile.c_str());
  367.      fstream r_w(NomeFile.c_str(), ios:: in | ios:: out);
  368.      r_w.seekp((id*x)+pos,ios_base::beg);
  369.      r_w >> quant;
  370.      quant=quant+agg;
  371.      r_w.seekp((id*x)+pos,ios_base::beg);
  372.      campo = Dimensiona(quant,DimCampo);
  373.      r_w << campo;
  374.      r_w.close();
  375.     }
  376. /************************************FINE**************************************/
  377. private:
  378. int FileSize(const char* sFileName)
  379. {
  380.   std::ifstream f;
  381.   f.open(sFileName, std::ios_base::binary | std::ios_base::in);
  382.   if (!f.good() || f.eof() || !f.is_open()) { return 0; }
  383.   f.seekg(0, std::ios_base::beg);
  384.   std::ifstream::pos_type begin_pos = f.tellg();
  385.   f.seekg(0, std::ios_base::end);
  386.   return static_cast<int>(f.tellg() - begin_pos);
  387. }
  388. /************************************FINE**************************************/
  389. string Dimensiona(float Num, int DimCampo)
  390. {
  391.     stringstream s;    
  392.     string Id;                  
  393.     s << Num;      
  394.     Id = s.str();    
  395.     Id.resize(DimCampo);
  396.     return Id;        
  397. }
  398. /************************************FINE**************************************/    
  399.    int GrandezzaRecord(string NomeFile)
  400.     {
  401.         string InteraRiga;
  402.         int dim;
  403.         ifstream apri(NomeFile.c_str(), ios::in);
  404.         if (apri)
  405.                  {
  406.                   getline(apri,InteraRiga,'\n');
  407.                   dim=InteraRiga.size()+2;
  408.                  }
  409.                  else
  410.                  cout << "File "<<NomeFile.c_str()<<" non trovato! "<< endl;
  411.                  
  412.         return dim;        
  413.     }
  414. /************************************FINE**************************************/      
  415.    char TrovaFlag(string NomeFile, int id)
  416.    {
  417.     char flag;
  418.     int x=GrandezzaRecord(NomeFile.c_str());
  419.     id--;
  420.     fstream read(NomeFile.c_str(), ios::in | ios::out);
  421.     if (read)
  422.        {
  423.         read.seekp((x*id)+6,ios_base::beg);
  424.         read >> flag;
  425.        }
  426.        else
  427.        cout << "Flag non trovato" << endl;
  428.     return flag;
  429.    }  
  430. /************************************FINE**************************************/  
  431.     void OrdinaFileChiave(string NomeFile, int DimChiave)
  432.     {  
  433.      string app,continuo,pun;
  434.      int i=0,il,iv,app1,salva;
  435.      int dim=GrandezzaRecord(NomeFile.c_str());
  436.      string resto,appoggio,scrivi;    
  437.      fstream read(NomeFile.c_str(),ios::in | ios::out);
  438.      if (read)
  439.         {
  440.          while(!read.eof())
  441.                            {
  442.                             read.seekp(dim*i , ios_base::beg);
  443.                             getline(read,Clog[i],';');              
  444.                             getline(read,Svet[i],'\0');
  445.                             read.seekp((dim*i)+DimChiave+3,ios_base::beg);
  446.                             read >> Ppunt[i];
  447.                             getline(read,continuo,'\n');
  448.                             i++;
  449.                            }                  
  450.          salva=i-1;
  451.          i--;
  452.          for (il = 0;il < i - 1; il++)
  453.          {
  454.           for (iv = il + 1; iv < i; iv++)
  455.               {
  456.                if (Svet[iv] < Svet[il])
  457.                  {
  458.                   appoggio = Svet[il];
  459.                   Svet[il] = Svet[iv];
  460.                   Svet[iv] = appoggio;
  461.             //    nuovo  
  462.                   app = Clog[il];
  463.                   Clog[il] = Clog[iv];
  464.                   Clog[iv] = app;
  465.            //                      
  466.                   app1= Ppunt[il];
  467.                   Ppunt[il]= Ppunt[iv];
  468.                   Ppunt[iv]= app1;
  469.                  }
  470.               }
  471.          }                            
  472.          //Scrivi file ordinato:
  473.          ofstream write(NomeFile.c_str(),ios::out);
  474.          for (int k=0;k<salva;k++)
  475.              {  
  476.               scrivi= Svet[k];  
  477.               scrivi.resize(DimChiave);
  478.               write << Clog[k]<<";"<< scrivi;
  479.               write << ";";
  480.               int pp=Ppunt[k];
  481.               pun=Dimensiona(pp,3);
  482.               write << pun << endl;
  483.              }
  484.          write.close();                                        
  485.         }
  486.         else
  487.         cout << "Impossibile aprire il file "<<NomeFile.c_str()<<" per ordinarlo!" << endl;
  488.     }
  489. /************************************FINE**************************************/  
  490. int CalcLog (int num)
  491. {
  492.   int result;
  493.   result = log2 (num);
  494.   return result;
  495. }  
  496. /************************************FINE**************************************/    
  497. };
  498. #endif