Advertisement
fcamuso

Corso recupero C++ - video 25

Nov 30th, 2022
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void stampa(string *p, int quanti)
  7. {
  8.   for (int i=0; i<quanti; i++)
  9.     cout << p[i] << endl;
  10. }
  11.  
  12. string *infinite_file_read(string nome_file, int &totale_righe, int &dimensione)
  13. {
  14.   dimensione = 5;
  15.   totale_righe = 0;
  16.   string *p = new string[dimensione];
  17.  
  18.   ifstream leggi(nome_file);
  19.   string riga = "";
  20.  
  21.   while ( getline(leggi, riga) )
  22.   {
  23.     if (totale_righe==dimensione)
  24.     {
  25.       string *temp = new string[dimensione*2];
  26.  
  27.       //copiamo le righe gia' lette in questo nuovo spazio
  28.       for (int i=0; i<dimensione; i++) temp[i] = p[i];
  29.  
  30.       dimensione *= 2;
  31.  
  32.       //restituiamo lo spazio per il vecchio array ormai saturo
  33.       delete[] p;
  34.       p = temp;
  35.     }
  36.  
  37.     p[totale_righe] = riga;
  38.     totale_righe++;
  39.   }
  40.  
  41.   leggi.close(); leggi.clear();
  42.  
  43.   return p;
  44.  
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50.   int totale_righe = 0;
  51.   int max_righe = 0;
  52.   string *p = infinite_file_read("dati.txt", totale_righe, max_righe);
  53.  
  54.   cout << max_righe << endl;
  55.   stampa(p, totale_righe);
  56.  
  57.  
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement