Advertisement
florin88

Scrivere in un file.txt

Mar 10th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. /*************************************************************/
  2. /*Nome Prog:  Scrivere in un file.txt                        */
  3. /*Questo semplice programma in C++, ti consente di scrivre all'interno di un file*/                                                  
  4. /*è importante generare prima il fileesempio.txt, altrimenti il programma restituirà un errore */
  5. /*Autore: florin88                                            */
  6. /*e-mail: ffinformaticus@gmail.com                            */
  7. /***********************************************************/
  8.  
  9. #include <fstream>
  10. #include <iostream>
  11. #include <string>  
  12.  
  13. using namespace std;  
  14.  
  15. int main() {
  16.  
  17.       string textLine;
  18.  
  19.       // come prima cosa implementiamo la funzione di apertura del file
  20.  
  21.       ifstream ifs("fileesempio.txt", ifstream::in);
  22.  
  23.       if (ifs.good())   {  
  24.  
  25.             // il programma continua, con la funzione che legge tutte le linee del fileesempio.txt
  26.  
  27.             while (!ifs.eof()) {
  28.  
  29.                   getline(ifs, textLine);
  30.  
  31.                   cout << textLine << endl;
  32.             }
  33.  
  34.             // concludiamo il programma e la scrittura con la chiusura del file  
  35.             ifs.close();
  36.  
  37.       } else
  38.  
  39.             // implementiamo la funzione che genera un messaggio in caso di errore o per meglio
  40.         // non riesce ad aprire il nostro fileesempio.txt
  41.  
  42.             cout << "ERRORE: impossibile aprire il file" << endl;  
  43.  
  44.       return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement