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 File - v.2.2 - HeaderToti.2.2.h

HeaderToti.2.2.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 GeneraID(string NomeFile,string N_FileID)
  26.     {
  27.      ifstream trovaID(N_FileID.c_str(), ios::in); //ID.txt file contentente il numero
  28.      if (trovaID)                         // dell'ultimo ID inserito
  29.               {
  30.                trovaID >> ID;
  31.                ID=ID+1;
  32.                trovaID.close();
  33.               }
  34.               else
  35.               ID=1;
  36.  
  37.      ofstream write(NomeFile.c_str(),ios::app);
  38.      string ss;
  39.      ss=Dimensiona(ID,5);
  40.      write <<ss<<";";
  41.      write <<"L;";  
  42.      write.close();
  43.  
  44.   // Aggiorna L'ID che viene scritto sul file ID.txt
  45.      ofstream aggID(N_FileID.c_str(), ios::out);  
  46.      aggID << ID;    
  47.      aggID.close();                    
  48.     }
  49. /************************************FINE**************************************/    
  50.     void ScriviCampo(string NomeFile, string Campo, int DimCampo)
  51.     {
  52.      ofstream write(NomeFile.c_str(),ios::app);
  53.      Campo.resize(DimCampo);
  54.      write << Campo;
  55.      write.close();    
  56.     }
  57. /************************************FINE**************************************/
  58.     void ScriviUltimoCampo(string NomeFile, string Campo, int DimCampo)
  59.     {
  60.      ofstream write(NomeFile.c_str(),ios::app);
  61.      Campo.resize(DimCampo);
  62.      write << Campo << endl;
  63.      write.close();    
  64.     }
  65. /************************************FINE**************************************/      
  66.     void ScriviCampoChiavePrimario(string NomeFile, string NomeFileChiave ,string Campo, int DimCampo)
  67.     {      
  68.      string controllo;
  69.      string flag;
  70.      ifstream leggi(NomeFileChiave.c_str(), ios::in);
  71.      if (leggi)
  72.                {
  73.                  while(!leggi.eof() & ViaLibera!=true)
  74.                                   {
  75.                                    getline(leggi,flag,';');                        
  76.                                    getline(leggi,controllo,'\0');
  77.                                    getline(leggi,punt,'\n');
  78.                                    if (controllo==Campo)
  79.                                     {
  80.                                      if (flag!="X")
  81.                                       {
  82.                                        cout << "Questo e' un campo chiave gia assegnato" << endl;
  83.                                        cout << "Scegline un'altro" << endl;
  84.                                        ViaLibera=true;
  85.                                       }
  86.                                     }
  87.                                   }
  88.                }
  89.      if (ViaLibera==false && !leggi)
  90.                 {
  91.                  int puntatoreID=ID-1;        
  92.                  ofstream write(NomeFileChiave.c_str(),ios::app);
  93.                  Campo.resize(DimCampo);
  94.                  write << " ;" << Campo;
  95.                  string idf=Dimensiona(puntatoreID,3);
  96.                  write << ";"<< idf << endl;
  97.                  write.close();
  98.                  ofstream scrivi(NomeFile.c_str(),ios::app);
  99.                  scrivi << Campo;
  100.                  scrivi.close();  
  101.                 }
  102.     }
  103. /************************************FINE**************************************/
  104.     void ScriviUltimoCampoChiavePrimario(string NomeFile, string NomeFileChiave ,string Campo, int DimCampo)
  105.     {      
  106.      string controllo;
  107.      string flag;
  108.      ifstream leggi(NomeFileChiave.c_str(), ios::in);
  109.      if (leggi)
  110.                {
  111.                  while(!leggi.eof() & ViaLibera!=true)
  112.                                   {
  113.                                    getline(leggi,flag,';');                        
  114.                                    getline(leggi,controllo,'\0');
  115.                                    getline(leggi,punt,'\n');
  116.                                    if (controllo==Campo)
  117.                                     {
  118.                                      if (flag!="X")
  119.                                       {
  120.                                        cout << "Questo e' un campo chiave gia assegnato" << endl;
  121.                                        cout << "Scegline un'altro" << endl;
  122.                                        ViaLibera=true;
  123.                                       }
  124.                                     }
  125.                                   }
  126.                }
  127.      if (ViaLibera==false && !leggi)
  128.                 {
  129.                  int puntatoreID=ID-1;        
  130.                  ofstream write(NomeFileChiave.c_str(),ios::app);
  131.                  Campo.resize(DimCampo);
  132.                  write << " ;" << Campo;
  133.                  string idf=Dimensiona(puntatoreID,3);
  134.                  write << ";"<< idf << endl;
  135.                  write.close();
  136.                  ofstream scrivi(NomeFile.c_str(),ios::app);
  137.                  scrivi << Campo<<endl;
  138.                  scrivi.close();  
  139.                 }
  140.     }
  141. /************************************FINE**************************************/
  142.     void ScriviChiaveSecond(string NomeFile, string NomeFileChiaveSecondario, string Campo, int DimCampo)
  143.     {
  144.      ofstream scrivi(NomeFileChiaveSecondario.c_str(),ios::app);
  145.      Campo.resize(DimCampo);
  146.      scrivi << " ;" << Campo;
  147.      string idf=Dimensiona(ID-1,3);
  148.      scrivi << ";" << idf << endl;
  149.      scrivi.close();          
  150.      ofstream write(NomeFile.c_str(), ios::app);
  151.      write << Campo;
  152.      write.close();  
  153.     }
  154. /************************************FINE**************************************/  
  155.     void ScriviUltimoCampoChiaveSecond(string NomeFile, string N_F_UltimoCampo, string Campo, int DimCampo)
  156.     {
  157.      ofstream write(N_F_UltimoCampo.c_str(),ios::app);
  158.      Campo.resize(DimCampo);
  159.      write << " ;" << Campo;
  160.      string idf=Dimensiona(ID-1,3);
  161.      write << ";" << idf << endl;
  162.      write.close();  
  163.      ofstream scrivi(NomeFile.c_str(),ios::app);
  164.      scrivi << Campo << endl;
  165.      scrivi.close();
  166.     }
  167. /************************************FINE**************************************/
  168.     void CancellazioneLogica(string NomeFile ,int id)
  169.     {
  170.      char flg =  TrovaFlag(NomeFile.c_str(),id);
  171.      if (flg!='X')
  172.      {
  173.       bool trova=false;
  174.       string IDcanc,restorecord;  
  175.       string ID=Dimensiona(id,5);
  176.       int k=0;
  177.       int q=GrandezzaRecord(NomeFile.c_str());
  178.    
  179.       fstream r_w(NomeFile.c_str(), ios::in | ios::out);
  180.       ID.resize(5);
  181.       while(!r_w.eof())
  182.       {                
  183.        getline(r_w,IDcanc,';');
  184.        if (ID==IDcanc)
  185.                    {
  186.                     trova=true;
  187.                     fstream write(NomeFile.c_str(), ios::in | ios::out);
  188.                     write.seekp((k*q)+6,ios_base::beg);
  189.                     write <<"X";
  190.                     write.close();
  191.                     cout << "Record Cancellato!."<< endl;
  192.                    }
  193.        getline(r_w,restorecord,'\n');  
  194.        k++;  
  195.       }
  196.       if (trova==false)
  197.                      cout << "ID da cancellare non presente nel file" <<NomeFile.c_str() << endl;      
  198.      }
  199.      else
  200.      cout << "Mi dispiace, ma il record numero "<<id<<" e' gia stato cancellato logicamente!" << endl;        
  201.     }  
  202. /************************************FINE**************************************/
  203. void CancellazioneLogicaChiave(string NomeFileChiave,string N_FileID, int id , int DimChiave)
  204.  {
  205.   int iidd=RiportaIDfile(N_FileID.c_str());
  206.   if (id<=iidd)
  207.   {
  208.    int x= GrandezzaRecord(NomeFileChiave.c_str());
  209.    int rig=0,puntatore,riga;
  210.    bool trov=false;
  211.    fstream r_w(NomeFileChiave.c_str(),ios::in | ios::out);
  212.    if (r_w)
  213.      {
  214.       while(!r_w.eof())
  215.                        {    
  216.                         r_w.seekp((x*rig)+DimChiave+3,ios_base::beg);
  217.                         r_w >> puntatore;
  218.                         puntatore++;
  219.                         if (puntatore==id)
  220.                                           {
  221.                                            riga=rig;
  222.                                            trov=true;
  223.                                            }                  
  224.                         rig++;
  225.                        }
  226.       r_w.close();                
  227.       if (trov==true)
  228.          {
  229.           fstream scrivi(NomeFileChiave.c_str(),ios::in | ios::out);          
  230.           scrivi.seekp((x*riga),ios_base::beg);
  231.           scrivi << "X";
  232.           scrivi.close();
  233.          }  
  234.          else
  235.          cout << "Id del file da cancellare non presente!" << endl;              
  236.      }
  237.      else
  238.      cout << "Impossibile aprire il file "<<NomeFileChiave.c_str()<< endl;
  239.   }
  240.   else
  241.    cout << "ID da cancellare non presente nel file" << NomeFileChiave.c_str()<< endl;
  242.  }
  243. /************************************FINE**************************************/
  244.     void StampaFile(string NomeFile)
  245.     {
  246.      system("CLS");
  247.      string Flag,Riga,IDRiga;
  248.      int k=0;  
  249.      int q=GrandezzaRecord(NomeFile.c_str());
  250.      ifstream read(NomeFile.c_str(),ios::in | ios::out);
  251.      if (read)
  252.               {
  253.                while(!read.eof())
  254.                 {
  255.                  getline(read,IDRiga,';');
  256.                  IDRiga.resize(5);              
  257.                  //ad.seekg(k*q+6,ios_base::beg);
  258.                  getline(read,Flag,';');
  259.                  getline(read,Riga,'\n');
  260.                  if (Flag=="L")
  261.                               {
  262.                                cout << IDRiga;
  263.                                cout << Riga << endl;
  264.                               }
  265.                  Flag=".";
  266.                  k++;                    
  267.                 }
  268.               }
  269.     }
  270. /************************************FINE**************************************/
  271.     void SequencialSearch(string NomeFilePrincipale, string NomeFileChiave, string Campo)
  272.     {
  273.      string confronto,Riga,Riga2,resto,ctr;  
  274.      int ID,puntID;
  275.      int x=GrandezzaRecord(NomeFilePrincipale.c_str());
  276.      bool trova=false;
  277.      system("CLS");
  278.      fstream read(NomeFileChiave.c_str(),ios::in | ios::out);
  279.      if (read)
  280.               {
  281.                while(!read.eof())
  282.                   {
  283.                    getline(read,ctr,';');
  284.                    getline(read,confronto,'\0');
  285.                    getline(read,resto,';');
  286.                    read >> puntID;
  287.                    getline(read,resto,'\n');
  288.                    if (confronto==Campo)
  289.                                         {
  290.                                          if (ctr!="X")
  291.                                           {                                                  
  292.                                            trova=true;
  293.                                            ID=puntID;
  294.                                            fstream leggi(NomeFilePrincipale.c_str(),ios::in | ios::out);
  295.                                            leggi.seekp(x*ID, ios_base::beg);
  296.                                            getline(leggi,Riga,';');
  297.                                            leggi.seekp((x*ID)+8,ios_base::beg);
  298.                                            getline(leggi,Riga2,'\n');
  299.                                            cout << Riga << Riga2 << endl;
  300.                                            leggi.close();
  301.                                           }
  302.                                         }  
  303.                    confronto="";                                      
  304.                   }
  305.                read.close();
  306.               }
  307.               else
  308.               cout << "File :"<<NomeFileChiave.c_str()<<" NON TROVATO!" << endl;
  309.              
  310.      if(trova==false)
  311.                     cout << "Campo da ricercare non presente nel File\n" << endl;                      
  312.     }
  313. /************************************FINE**************************************/
  314.    void BinarySearch(string NomeFile, string NomeFileChiave , string Campo, int DimChiave)
  315.    {
  316.    // Da utilizzare per trovare un campo UNIVOCO
  317.     system("CLS");  
  318.     string ricerca,continuo,ctr,stringa,stringa1;
  319.     bool esci=true,trovat=false;
  320.     OrdinaFileChiave(NomeFileChiave.c_str(), DimChiave);
  321.     int x = FileSize(NomeFileChiave.c_str());
  322.     int gr = GrandezzaRecord(NomeFileChiave.c_str());
  323.     int Rinf,Rsup,punta,punt,i,puntatore,puntabyte;
  324.     Rinf = 0;
  325.     i = 0;
  326.     Rsup = x/gr;
  327.     int N_acces = CalcLog(Rsup);
  328.     fstream read("FileOrdinato.txt", ios::in | ios::out);
  329.     do
  330.     {
  331.      if (read)
  332.      {
  333.       punta=(Rinf+Rsup) / 2;
  334.       puntabyte= gr*punta;
  335.       read.seekp(puntabyte,ios_base::beg);
  336.       getline(read,ctr,';');
  337.       getline(read,ricerca,'\0');
  338.       getline(read,continuo,';');
  339.       read >> punt;  
  340.       if (Campo>ricerca)        
  341.                       Rinf = punta;
  342.                       else
  343.                       if (Campo<ricerca)
  344.                                         Rsup=punta;                      
  345.                       else
  346.                       {
  347.                       if (ctr!="X")
  348.                          {
  349.                          esci=false;
  350.                          trovat=true;
  351.                          puntatore=punt;
  352.                          }
  353.                       }
  354.       i++;  
  355.       if (i>N_acces)
  356.                   esci=false;            
  357.      }  
  358.     }
  359.     while(esci);
  360.    
  361.     if (trovat==true)  
  362.        {
  363.         int gr1 = GrandezzaRecord(NomeFile.c_str());
  364.         gr1=gr1*puntatore;            
  365.         fstream leggi(NomeFile.c_str(),ios::in | ios::out);
  366.         leggi.seekp(gr1, ios_base::beg);
  367.         getline(leggi,stringa,';');
  368.         leggi.seekp((gr1)+8,ios_base::beg);
  369.         getline(leggi,stringa1,'\n');
  370.         cout << stringa << stringa1 << endl;        
  371.        }  
  372.        else
  373.        cout << "Campo da ricercare non TROVATO!. " << endl;
  374.     read.close();
  375.     system("DEL FileOrdinato.txt");
  376.    }
  377. /************************************FINE**************************************/
  378.     void ModificaCampo(string NomeFile,string N_FileID,int id,int pos, int DimCampo)
  379.     {      // Modifica di un campo NORMALE
  380.      int iidd=RiportaIDfile(N_FileID.c_str());
  381.      if (id<=iidd)
  382.      {
  383.       string Modificato;
  384.       char y = TrovaFlag(NomeFile.c_str(),id);
  385.       if (y=='L')
  386.         {
  387.          cout << "Inserisci il campo modificato: ";
  388.          cin.sync();
  389.          getline(cin,Modificato);
  390.          Modificato.resize(DimCampo);
  391.          fstream write(NomeFile.c_str(),ios::in | ios::out);
  392.          int x=GrandezzaRecord(NomeFile.c_str());
  393.          id=id-1;
  394.          write.seekp((id*x)+pos,ios_base::beg);
  395.          write << Modificato;
  396.          write.close();
  397.          cout << "Campo Modificato!" << endl;
  398.         }
  399.         else
  400.         {
  401.          system("CLS");  
  402.          cout << "Impossibile modificare!" << endl;
  403.          cout << "Il record e' stato cancellato logicamente" << endl<< endl;
  404.          cout << "Premi INVIO per continuare" << endl;
  405.          getchar();
  406.          getchar();
  407.         }
  408.      }
  409.      else
  410.      cout << "L'ID inserito non esiste" << endl;
  411.     }
  412. /************************************FINE**************************************/
  413.     void ModificaCampoChiavePrim(string NomeFile, string NomeFileChiave,string N_FileID, int id, int pos, int DimChiave)
  414.     {
  415.      int aapp=RiportaIDfile(N_FileID.c_str());
  416.      if (id<=aapp)
  417.      {
  418.       int i, puntId,riga,gr;
  419.       i = 0;
  420.       string flag,flagriga,modificato;
  421.       bool trovato=false, ok;
  422.       gr=GrandezzaRecord(NomeFileChiave.c_str());
  423.       fstream r_w(NomeFileChiave.c_str(), ios::in | ios::out);
  424.       while(!r_w.eof())
  425.         {
  426.          r_w.seekp((i*gr),ios_base::beg);
  427.          getline(r_w,flag,';');            
  428.          r_w.seekp((i*gr)+DimChiave+3,ios_base::beg);
  429.          r_w >> puntId;
  430.          puntId=puntId+1;
  431.          if (id==puntId)
  432.                           {
  433.                            trovato=true;
  434.                            riga=i;
  435.                            flagriga=flag;
  436.                           }
  437.          i++;                
  438.         }
  439.       r_w.close();
  440.      
  441.       if (trovato==true & flagriga!="X")
  442.         {
  443.          do
  444.          {
  445.           ok = false;                        
  446.           cout << "Inserisci il campo modificato: " << endl;
  447.           cin.sync();
  448.           getline(cin,modificato);
  449.           // Controllo Chiave      
  450.           string controllo,flag,resto;  
  451.           ifstream leggi(NomeFileChiave.c_str(), ios::in);
  452.           if (leggi)
  453.                {
  454.                  while(!leggi.eof() & ok!=true)  
  455.                                   {
  456.                                    getline(leggi,flag,';');                        
  457.                                    getline(leggi,controllo,'\0');
  458.                                    getline(leggi,resto,'\n');
  459.                                    if (controllo==modificato)
  460.                                     {
  461.                                      if (flag!="X")
  462.                                       {
  463.                                        cout << "Questo e' un campo chiave gia assegnato" << endl;
  464.                                        cout << "Scegline un'altro" << endl;
  465.                                        ok=true;
  466.                                       }
  467.                                     }
  468.                                   }
  469.                }        
  470.           // Fine Controllo Chiave  
  471.           }
  472.          while(ok);      
  473.          modificato.resize(DimChiave);
  474.          fstream scrivi(NomeFileChiave.c_str(), ios::in | ios::out);
  475.          scrivi.seekp((riga*gr)+2,ios_base::beg);
  476.          scrivi << modificato;
  477.          scrivi.close();
  478.          
  479.          fstream write(NomeFile.c_str(), ios::in | ios::out);
  480.          gr=GrandezzaRecord(NomeFile.c_str());
  481.          write.seekp((gr*riga)+pos,ios_base::beg);
  482.          write << modificato;
  483.          write.close();
  484.          cout << "Campo modificato!"<< endl;
  485.         }
  486.         else
  487.         if (flagriga=="X")
  488.            cout << "Il record e' stato cancellato logicamente" << endl;
  489.            else
  490.            cout << "ID non trovato!" << endl;  
  491.      }
  492.      else
  493.      cout << "ID inserito non presente nel file" << endl;            
  494.     }
  495. /************************************FINE**************************************/
  496.     void ModificaCampoChiaveSecond(string NomeFile, string NomeFileChiave,string N_FileID, int id, int pos, int DimChiave)
  497.     {
  498.      int aapp=RiportaIDfile(N_FileID.c_str());
  499.      if (id<=aapp)
  500.      {    
  501.       int i, puntId,riga,gr;
  502.       i = 0;
  503.       string flag,flagriga,modificato;
  504.       bool trovato=false;
  505.       gr=GrandezzaRecord(NomeFileChiave.c_str());
  506.       fstream r_w(NomeFileChiave.c_str(), ios::in | ios::out);
  507.       while(!r_w.eof())
  508.         {
  509.          r_w.seekp((i*gr),ios_base::beg);
  510.          getline(r_w,flag,';');            
  511.          r_w.seekp((i*gr)+DimChiave+3,ios_base::beg);
  512.          r_w >> puntId;
  513.          puntId=puntId+1;
  514.          if (id==puntId)
  515.                           {
  516.                            trovato=true;
  517.                            riga=i;
  518.                            flagriga=flag;
  519.                           }
  520.          i++;                
  521.         }
  522.       r_w.close();
  523.      
  524.       if (trovato==true & flagriga!="X")
  525.         {
  526.          cout << "Inserisci il campo modificato: " << endl;
  527.          cin.sync();
  528.          getline(cin,modificato);
  529.          modificato.resize(DimChiave);
  530.          fstream scrivi(NomeFileChiave.c_str(), ios::in | ios::out);
  531.          scrivi.seekp((riga*gr)+2,ios_base::beg);
  532.          scrivi << modificato;
  533.          scrivi.close();
  534.          
  535.          fstream write(NomeFile.c_str(), ios::in | ios::out);
  536.          gr=GrandezzaRecord(NomeFile.c_str());
  537.          write.seekp((gr*riga)+pos,ios_base::beg);
  538.          write << modificato;
  539.          write.close();
  540.          cout << "Campo modificato!"<< endl;
  541.         }
  542.         else
  543.         {
  544.          if (flagriga=="X")
  545.            cout << "Il record e' stato cancellato logicamente" << endl;
  546.            else
  547.            cout << "ID non trovato!" << endl;
  548.         }
  549.      }
  550.      else
  551.      cout << "ID inserito non presente nel file" << endl;  
  552.     }
  553. /************************************FINE**************************************/
  554.     void AggiungiQuantita(string NomeFile, float agg, int id, int pos, int DimCampo)
  555.     {
  556.      float quant;
  557.      string campo;
  558.      id = id - 1;
  559.      int x=GrandezzaRecord(NomeFile.c_str());
  560.      fstream r_w(NomeFile.c_str(), ios:: in | ios:: out);
  561.      r_w.seekp((id*x)+pos,ios_base::beg);
  562.      r_w >> quant;
  563.      quant=quant+agg;
  564.      r_w.seekp((id*x)+pos,ios_base::beg);
  565.      campo = Dimensiona(quant,DimCampo);
  566.      r_w << campo;
  567.      r_w.close();
  568.     }
  569. /************************************FINE**************************************/
  570.     void CancFisicaFileP(string NomeFile, string N_FileID)
  571.     {
  572.      int DimRec=GrandezzaRecord(NomeFile.c_str());
  573.      int i=0;
  574.      int iidd;
  575.      int xy = FileSize(NomeFile.c_str());
  576.      int i2=xy/DimRec;
  577.      string flag,rigaintera,resto;
  578.                                      
  579.      ifstream leggi(N_FileID.c_str(),ios::in);
  580.      leggi >>  iidd;
  581.      leggi.close();      
  582.      fstream file(NomeFile.c_str(),ios::in | ios::out);
  583.      if (file)
  584.         {
  585.           while(i<i2)
  586.                  {
  587.                                              
  588.                   file.seekp((DimRec*i)+6,ios_base::beg);
  589.                   getline(file,flag,';');
  590.                   if (flag=="X")
  591.                                 {
  592.                                  i++;
  593.                                 }
  594.                                 else
  595.                                 {
  596.                                  file.seekp((DimRec*i),ios_base::beg);
  597.                                  getline(file,rigaintera,'\n');
  598.                                  ofstream write("FileApp1.txt",ios::app);
  599.                                  write << rigaintera << endl;
  600.                                  i++;
  601.                                 }
  602.                  }
  603.         file.close();  
  604.   /* INIZIO RINOMINA FILE */
  605.  
  606.    if( remove(NomeFile.c_str()) != 0 ) // cancella file principale
  607.                                 perror( "Error deleting file" ); // per
  608.                                 else    // sostituirlo con il nuovo
  609.                                 puts( "File successfully deleted" );
  610.   int result;
  611.   char oldname[] ="FileApp1.txt";
  612.   result= rename( oldname , NomeFile.c_str() );
  613.   if ( result == 0 )
  614.               puts ( "File successfully renamed" );
  615.               else
  616.                perror( "Error renaming file" );
  617.   /* FINE RINOMINA FILE */
  618.         }
  619.         else
  620.         cout << "Impossibile aprire il file "<<NomeFile.c_str()<< endl;
  621.     }
  622. /************************************FINE**************************************/
  623. /*******************************************************tiz!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  624. float EstraiNumero(string NomeFile,int pos,int id)
  625. {
  626.     char y = TrovaFlag(NomeFile.c_str(),id);
  627.     float Numero;
  628.     if (y=='L')
  629.     {
  630.                int x=GrandezzaRecord(NomeFile.c_str());
  631.                id--;
  632.                fstream read(NomeFile.c_str(),ios::in | ios::out);
  633.                read.seekp((id*x)+pos,ios_base::beg);
  634.                read >> Numero;
  635.                return Numero;
  636.     }
  637.     else
  638.     {
  639.                system("CLS");
  640.                cout << "Impossibile estrarre il numero!" << endl;
  641.                cout << "Il record e' stato cancellato logicamente" << endl<< endl;
  642.                cout << "Premi INVIO per continuare" << endl;getchar();getchar();
  643.     }
  644. }
  645. /************************************FINE**************************************/
  646.     void CancFisicaFileSec(string NomeFile, string N_FileID)
  647.     {
  648.      int DimRec=GrandezzaRecord(NomeFile.c_str());
  649.      int i=0;
  650.      int iidd;
  651.      int xy = FileSize(NomeFile.c_str());
  652.      int i2=xy/DimRec;
  653.      string flag,rigaintera,resto;                                  
  654.      ifstream leggi(N_FileID.c_str(),ios::in);
  655.      leggi >>  iidd;
  656.      leggi.close();      
  657.      fstream file(NomeFile.c_str(),ios::in | ios::out);
  658.      if (file)
  659.         {
  660.           while(i<i2)
  661.                  {
  662.                                              
  663.                   file.seekp((DimRec*i),ios_base::beg);
  664.                   getline(file,flag,';');
  665.                   if (flag=="X")
  666.                                 {
  667.                                  i++;
  668.                                 }
  669.                                 else
  670.                                 {
  671.                                  file.seekp((DimRec*i),ios_base::beg);
  672.                                  getline(file,rigaintera,'\n');
  673.                                  ofstream write("FileApp1.txt",ios::app);
  674.                                  write << rigaintera << endl;
  675.                                  i++;
  676.                                 }
  677.                  }
  678.         file.close();  
  679.   /* INIZIO RINOMINA FILE */
  680.  
  681.    if( remove(NomeFile.c_str()) != 0 ) // cancella file principale
  682.                                 perror( "Error deleting file" ); // per
  683.                                 else    // sostituirlo con il nuovo
  684.                                 puts( "File successfully deleted" );
  685.   int result;
  686.   char oldname[] ="FileApp1.txt";
  687.   result= rename( oldname , NomeFile.c_str() );
  688.   if ( result == 0 )
  689.               puts ( "File successfully renamed" );
  690.               else
  691.                perror( "Error renaming file" );
  692.   /* FINE RINOMINA FILE */
  693.         }
  694.         else
  695.         cout << "Impossibile aprire il file "<<NomeFile.c_str()<< endl;
  696.     }
  697. /************************************FINE**************************************/
  698.     void AggIdFileP(string NomeFile)
  699.     {
  700.      int i=0;
  701.      string IDagg;
  702.      int ncar=FileSize(NomeFile.c_str());
  703.      int DimRec=GrandezzaRecord(NomeFile.c_str());
  704.      int numID=ncar/DimRec;
  705.      fstream aggior(NomeFile.c_str(),ios::in | ios::out);
  706.      if (aggior)
  707.         {
  708.          while(i<numID)
  709.              {
  710.               IDagg=Dimensiona(i+1,5);
  711.               aggior.seekp(i*DimRec,ios_base::beg);
  712.               aggior << IDagg;
  713.               i++;
  714.              }
  715.         }
  716.     }
  717. /************************************FINE**************************************/
  718.     void AggIdFileSec(string NomeFile, int DimCampo)
  719.     {
  720.      int i=0;
  721.      string IDagg;
  722.      int ncar=FileSize(NomeFile.c_str());
  723.      int DimRec=GrandezzaRecord(NomeFile.c_str());
  724.      int numID=ncar/DimRec;
  725.      fstream aggior(NomeFile.c_str(),ios::in | ios::out);
  726.      if (aggior)
  727.         {
  728.          while(i<numID)
  729.              {
  730.               IDagg=Dimensiona(i,3);
  731.               aggior.seekp((i*DimRec)+3+DimCampo,ios_base::beg);
  732.               aggior << IDagg;
  733.               i++;
  734.              }
  735.         }
  736.     }
  737. /************************************FINE**************************************/
  738.     void AggiornaID(string NomeFilePrincipale,string N_FileID)
  739.     {
  740.      int ncar=FileSize(NomeFilePrincipale.c_str());
  741.      int DimRec=GrandezzaRecord(NomeFilePrincipale.c_str());
  742.      int numID=ncar/DimRec;
  743.      ofstream write(N_FileID.c_str(),ios::out);
  744.      write << numID;
  745.      write.close();
  746.     }
  747. /************************************FINE**************************************/
  748. private:
  749. int FileSize(const char* sFileName)
  750. {
  751.   std::ifstream f;
  752.   f.open(sFileName, std::ios_base::binary | std::ios_base::in);
  753.   if (!f.good() || f.eof() || !f.is_open()) { return 0; }
  754.   f.seekg(0, std::ios_base::beg);
  755.   std::ifstream::pos_type begin_pos = f.tellg();
  756.   f.seekg(0, std::ios_base::end);
  757.   return static_cast<int>(f.tellg() - begin_pos);
  758. }
  759. /************************************FINE**************************************/
  760. string Dimensiona(float Num, int DimCampo)
  761. {
  762.     stringstream s;    
  763.     string Id;                  
  764.     s << Num;      
  765.     Id = s.str();    
  766.     Id.resize(DimCampo);
  767.     return Id;        
  768. }
  769. /************************************FINE**************************************/    
  770.    int GrandezzaRecord(string NFil)
  771.     {
  772.         string InteraRiga;
  773.         int dim;
  774.         ifstream apri(NFil.c_str(), ios::in);
  775.         if (apri)
  776.                  {
  777.                   getline(apri,InteraRiga,'\n');
  778.                   dim=InteraRiga.size()+2;
  779.                  }
  780.                  else
  781.                  cout << "File "<<NFil.c_str()<<" non trovato! "<< endl;
  782.                  
  783.         return dim;        
  784.     }
  785. /************************************FINE**************************************/      
  786.    char TrovaFlag(string NomeFile, int id)
  787.    {
  788.     char flag;
  789.     int x=GrandezzaRecord(NomeFile.c_str());
  790.     id--;
  791.     fstream read(NomeFile.c_str(), ios::in | ios::out);
  792.     if (read)
  793.        {
  794.         read.seekp((x*id)+6,ios_base::beg);
  795.         read >> flag;
  796.        }
  797.        else
  798.        cout << "Flag non trovato" << endl;
  799.     return flag;
  800.    }  
  801. /************************************FINE**************************************/  
  802.     void OrdinaFileChiave(string NomeFile, int DimChiave)
  803.     {  
  804.      string app,continuo,pun;
  805.      int i=0,il,iv,app1,salva;
  806.      int dim=GrandezzaRecord(NomeFile.c_str());
  807.      string resto,appoggio,scrivi;    
  808.      fstream read(NomeFile.c_str(),ios::in | ios::out);
  809.      if (read)
  810.         {
  811.          while(!read.eof())
  812.                            {
  813.                             read.seekp(dim*i , ios_base::beg);
  814.                             getline(read,Clog[i],';');              
  815.                             getline(read,Svet[i],'\0');
  816.                             read.seekp((dim*i)+DimChiave+3,ios_base::beg);
  817.                             read >> Ppunt[i];
  818.                             getline(read,continuo,'\n');
  819.                             i++;
  820.                            }                  
  821.          salva=i-1;
  822.          i--;
  823.          for (il = 0;il < i - 1; il++)
  824.          {
  825.           for (iv = il + 1; iv < i; iv++)
  826.               {
  827.                if (Svet[iv] < Svet[il])
  828.                  {
  829.                   appoggio = Svet[il];
  830.                   Svet[il] = Svet[iv];
  831.                   Svet[iv] = appoggio;
  832.             //    nuovo  
  833.                   app = Clog[il];
  834.                   Clog[il] = Clog[iv];
  835.                   Clog[iv] = app;
  836.            //                      
  837.                   app1= Ppunt[il];
  838.                   Ppunt[il]= Ppunt[iv];
  839.                   Ppunt[iv]= app1;
  840.                  }
  841.               }
  842.          }                            
  843.          //Scrivi file ordinato:
  844.          ofstream write("FileOrdinato.txt",ios::out);
  845.          for (int k=0;k<salva;k++)
  846.              {  
  847.               scrivi= Svet[k];  
  848.               scrivi.resize(DimChiave);
  849.               write << Clog[k]<<";"<< scrivi;
  850.               write << ";";
  851.               int pp=Ppunt[k];
  852.               pun=Dimensiona(pp,3);
  853.               write << pun << endl;
  854.              }
  855.          write.close();                                        
  856.         }
  857.         else
  858.         cout << "Impossibile aprire il file "<<NomeFile.c_str()<<" per ordinarlo!" << endl;
  859.     }
  860. /************************************FINE**************************************/  
  861. int CalcLog (int num)
  862. {
  863.   int result;
  864.   result = log2 (num);
  865.   return result;
  866. }  
  867. /************************************FINE**************************************/  
  868. int RiportaIDfile(string NomeFile)
  869. {
  870.   int id;
  871.   ifstream read(NomeFile.c_str(),ios::in);
  872.   if (read)
  873.      {
  874.       read >> id;
  875.      }
  876.      else
  877.      cout << "Impossibile aprire il file "<<NomeFile.c_str()<<" per trovare l'ID" << endl;
  878.      
  879.   return id;  
  880. }  
  881. /************************************FINE**************************************/
  882. };
  883. #endif