Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
fine esperimenti io formattato
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-cat-tty committed Dec 20, 2021
1 parent 42c04c2 commit 9f8b321
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 0 deletions.
9 changes: 9 additions & 0 deletions esercitazioni/append.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
azzo
mazzo
pazzo
seconda
scruttira
su file
questa
è la terza
direi sia abbastanza
25 changes: 25 additions & 0 deletions esercitazioni/leggi_numero_scarta_caratteri.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>

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<std::streamsize>::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;
}
}
1 change: 1 addition & 0 deletions esercitazioni/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ciao
7 changes: 7 additions & 0 deletions esercitazioni/redirect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a
asdfa
sdf asf
abba
abba azzo
ozzo lollo

18 changes: 18 additions & 0 deletions esercitazioni/redirect_caratteri_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <fstream>

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;
}
38 changes: 38 additions & 0 deletions esercitazioni/redirect_caratteri_file_lettura.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <fstream>

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;
}
11 changes: 11 additions & 0 deletions esercitazioni/redirect_caratteri_io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

using namespace std;

int main() {
char c;
cin >> noskipws;
while (cin >> c)
cout << c;
return 0;
}
16 changes: 16 additions & 0 deletions esercitazioni/somma_istream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
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;
}
22 changes: 22 additions & 0 deletions esercitazioni/stream_a_funzione.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <fstream>

using namespace std;

bool scrivi(ostream &s, const char *str) {
if (!s) {
cerr << "Errore nell'apertura";
return 1;
}

s << str;

return static_cast<bool>(s);
}

int main() {
ofstream f("out.txt");
scrivi(f, "ciao\n");
scrivi(cout, "ciao\n");
f.close();
}
20 changes: 20 additions & 0 deletions esercitazioni/svuotamenti_forzati_cout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <cstdlib>

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;
}
19 changes: 19 additions & 0 deletions esercitazioni/write_append.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <fstream>

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;
}

0 comments on commit 9f8b321

Please sign in to comment.