diff --git a/esercitazioni/append.txt b/esercitazioni/append.txt new file mode 100644 index 0000000..5da1d26 --- /dev/null +++ b/esercitazioni/append.txt @@ -0,0 +1,9 @@ +azzo +mazzo +pazzo +seconda +scruttira +su file +questa +è la terza +direi sia abbastanza diff --git a/esercitazioni/leggi_numero_scarta_caratteri.cpp b/esercitazioni/leggi_numero_scarta_caratteri.cpp new file mode 100644 index 0000000..384f3bc --- /dev/null +++ b/esercitazioni/leggi_numero_scarta_caratteri.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main() { + int i; + cout << "Immettere un numero: "; + while (!(cin>>i)) { + if (cin.eof()) + break; + cin.clear(); + cin.ignore(); // scarta carttere successivo + // posso scartare una riga intera con cin.igonre(srd::numeric_limits::max(), '\n'); + } + + // sono riuscito a leggere un numero oppure è arrivato eof + if (cin.eof()) { + cerr << "Lettura fallita" << endl; + return 1; + } + else { + cout << "Numero letto: " << i << endl; + return 0; + } +} diff --git a/esercitazioni/out.txt b/esercitazioni/out.txt new file mode 100644 index 0000000..887ae93 --- /dev/null +++ b/esercitazioni/out.txt @@ -0,0 +1 @@ +ciao diff --git a/esercitazioni/redirect.txt b/esercitazioni/redirect.txt new file mode 100644 index 0000000..0c41f7d --- /dev/null +++ b/esercitazioni/redirect.txt @@ -0,0 +1,7 @@ +a +asdfa +sdf asf +abba +abba azzo +ozzo lollo + diff --git a/esercitazioni/redirect_caratteri_file.cpp b/esercitazioni/redirect_caratteri_file.cpp new file mode 100644 index 0000000..9146e36 --- /dev/null +++ b/esercitazioni/redirect_caratteri_file.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; + +int main() { + char c; + ofstream f("redirect.txt"); + if (!f) { + cerr << "Errore apertura"; + return 1; + } + cin >> noskipws; + while (cin >> c) + f << c; + f.close(); + return 0; +} diff --git a/esercitazioni/redirect_caratteri_file_lettura.cpp b/esercitazioni/redirect_caratteri_file_lettura.cpp new file mode 100644 index 0000000..cc845a1 --- /dev/null +++ b/esercitazioni/redirect_caratteri_file_lettura.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +const char filename[] = "redirect.txt"; + +int main() { + char c; + ofstream fo(filename); + + if (!fo) { + + cerr << "Errore apertura"; + return 1; + } + + cin >> noskipws; + while (cin >> c) + fo << c; + + fo.close(); + + ifstream fi(filename); + + if (!fi) { + cerr << "Errore apertura file in lettura"; + return 1; + } + + fi >> noskipws; + while (fi >> c) + cout << c; + + fi.close(); + + return 0; +} diff --git a/esercitazioni/redirect_caratteri_io.cpp b/esercitazioni/redirect_caratteri_io.cpp new file mode 100644 index 0000000..d16a342 --- /dev/null +++ b/esercitazioni/redirect_caratteri_io.cpp @@ -0,0 +1,11 @@ +#include + +using namespace std; + +int main() { + char c; + cin >> noskipws; + while (cin >> c) + cout << c; + return 0; +} diff --git a/esercitazioni/somma_istream.cpp b/esercitazioni/somma_istream.cpp new file mode 100644 index 0000000..5b8d73f --- /dev/null +++ b/esercitazioni/somma_istream.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main() { + int i, somma = 0; + while (cin >> i) { + somma += i; + } + cout << "Stato cin: " << !(!cin) << endl; + cout << "Raggiunto EOF? " << cin.eof() << endl; + cout << "Pulizia cin" << endl; cin.clear(); + cout << "Stato cin: " << !(!cin) << endl; + cout << "Raggiunto EOF? " << cin.eof() << endl; + cout << "Somma: " << somma << endl; + return 0; +} diff --git a/esercitazioni/stream_a_funzione.cpp b/esercitazioni/stream_a_funzione.cpp new file mode 100644 index 0000000..405679f --- /dev/null +++ b/esercitazioni/stream_a_funzione.cpp @@ -0,0 +1,22 @@ +#include +#include + +using namespace std; + +bool scrivi(ostream &s, const char *str) { + if (!s) { + cerr << "Errore nell'apertura"; + return 1; + } + + s << str; + + return static_cast(s); +} + +int main() { + ofstream f("out.txt"); + scrivi(f, "ciao\n"); + scrivi(cout, "ciao\n"); + f.close(); +} diff --git a/esercitazioni/svuotamenti_forzati_cout.cpp b/esercitazioni/svuotamenti_forzati_cout.cpp new file mode 100644 index 0000000..2ffa9c7 --- /dev/null +++ b/esercitazioni/svuotamenti_forzati_cout.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace std; + +int main() { + int i; + cout << "Azz "; system("sleep 2"); cin >> i; // sync i/o + + for (int i=0; i<1000; i++) cout << "mazzopazzolazzo "; // scrittura chunk e riempimento buffer + + system("sleep 2"); + + cout << " Endline in arrivo " << endl; // '\n' alla fine dello stream di byte + + cout << " Flush forzato " << flush; + + cout << " Terminazione naturale"; + return 0; +} diff --git a/esercitazioni/write_append.cpp b/esercitazioni/write_append.cpp new file mode 100644 index 0000000..63b79a4 --- /dev/null +++ b/esercitazioni/write_append.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main() { + const char filename[] = "append.txt"; + fstream f(filename, ios_base::app); + if (!f) { + cerr << "errore apertura file"; + return 1; + } + cin >> noskipws; + char c; + while (cin >> c) + f << c; + f.close(); + return 0; +}