{"id":107,"date":"2021-01-12T01:19:38","date_gmt":"2021-01-12T00:19:38","guid":{"rendered":"http:\/\/materiale.gabrielemottola.it\/?p=107"},"modified":"2021-01-12T11:52:07","modified_gmt":"2021-01-12T10:52:07","slug":"lettura-di-dati-da-file-di-testo","status":"publish","type":"post","link":"https:\/\/materiale.gabrielemottola.it\/index.php\/2021\/01\/12\/lettura-di-dati-da-file-di-testo\/","title":{"rendered":"Lettura di dati da file di testo"},"content":{"rendered":"\n<p>In c++ ci sono pi\u00f9 modi per leggere i file di testi.<\/p>\n\n\n\n<h2>fstream &#8211; lettura diretta<\/h2>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">char buffer[100]; \/\/stringa che contiene la nostra parola\nfstream input; \/\/creamo lo stram per il file\ninput.open(file, ios::in); \/\/apriamo lo stream in modalit\u00e0 in per la lettura. file \u00e8 la stringa con l'url, ricordati di impostare il tuo\n if(!input.fail())\n     {      \n       while(!input.eof())\n     {\n       if(input&gt;&gt;buffer){\n         \/\/Ora dentro buffer ci sar\u00e0 la nostra parola\n         }       \n     }\n     }\n   input.close();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/uso degli stream\nmyin >> a; myout &lt;&lt; x; myin.get(c); myapp.put(c);<\/code><\/pre>\n\n\n\n<h2>Varianti di apertura di uno stream<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">myin.open(\"ingresso.txt\",ios::in); \/\/lettura     \nmyout.open(\"uscita.txt\",ios::out); \/\/scrittura     \nmyapp.open(\"uscita2.txt\",ios::out|ios::app); \/\/app. <\/code><\/pre>\n\n\n\n<p>Gestione degl errori<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/\u00c8 desiderabile poter passare i nomi dei file al programma:\n\/\/Es:> .\/a.out pippo pluto=\u21d2nomi dei file passati tramite argc e argv:\nint main (intargc,char*argv[]){\nfstream myin,myout;myin.open(argv[1],ios::in);\nmyout.open(argv[2],ios::out);\n\/\/\u00c8 necessario gestire l\u2019errore utente e la mancata apertura:\nif(argc!=3) {cerr &lt;&lt; \"Usage:.\/a.out&lt;source>&lt;target>\\n\";exit(0);}if(myin.fail()) {\ncerr &lt;&lt; \"Ilfile\" &lt;&lt; argv[1] &lt;&lt; \"nonesiste\\n\";exit(0);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In c++ ci sono pi\u00f9 modi per leggere i file di testi. fstream &#8211; lettura diretta Varianti di apertura di uno stream Gestione degl errori<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":4,"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/materiale.gabrielemottola.it\/index.php\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}