it:informatica:linguaggio_c:scrittura_e_lettura_file_binario
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/scrittura_e_lettura_file_binario?do=diff&rev2%5B0%5D=&rev2%5B1%5D=1551188125&difftype=sidebyside
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | it:informatica:linguaggio_c:scrittura_e_lettura_file_binario [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Scrittura e lettura file binario (fwrite e fread) ====== | ||
+ | **Concetti: | ||
+ | Funzioni '' | ||
+ | |||
+ | **Testo: | ||
+ | Dopo aver riempito un vettore di strutture (i cui elementi della struttura sono un vettore di caratteri contenente il nome di uno studente, una variabile di tipo '' | ||
+ | * Aprire in scrittura un file di nome "'' | ||
+ | * Scrivere nel file in modo binario, utilizzando la funzione '' | ||
+ | * Chiudere il file | ||
+ | * Riaprire il file in lettura | ||
+ | * Leggerlo utilizzando la funzione '' | ||
+ | * Stampare tutti i dati contenuti nel vettore di strutture | ||
+ | * Chiudere il file | ||
+ | |||
+ | **Soluzione: | ||
+ | <file C file_binario_fwrite_fread.c> | ||
+ | /* Scrittura e lettura di file binari (fwrite e fread) */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define LUN 50 | ||
+ | #define N_STUD 2 | ||
+ | |||
+ | typedef struct stud{ | ||
+ | char nome[LUN+1]; | ||
+ | unsigned int matricola; | ||
+ | float media; | ||
+ | }stud_t; | ||
+ | |||
+ | |||
+ | int main() { | ||
+ | FILE *fp; | ||
+ | stud_t studente[N_STUD]; | ||
+ | int i; | ||
+ | char riga[LUN+1]; | ||
+ | unsigned int n_stud; | ||
+ | |||
+ | strcpy(studente[0].nome, | ||
+ | studente[0].matricola = 120000; | ||
+ | studente[0].media = 25.5; | ||
+ | |||
+ | strcpy(studente[1].nome, | ||
+ | studente[1].matricola = 120001; | ||
+ | studente[1].media = 28.5; | ||
+ | |||
+ | fp = fopen(" | ||
+ | if (fp == NULL){ | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | |||
+ | /* Scrittura del file */ | ||
+ | /* NOTA 2: un file binario non e' detto che sia sempre leggibile | ||
+ | in modo corretto su una calcolatore differente da quello su | ||
+ | cui e' stato generato, perche' | ||
+ | | ||
+ | */ | ||
+ | |||
+ | /* Scrittura in binario di tutti i record */ | ||
+ | fwrite(studente, | ||
+ | |||
+ | /* DIMENSIONE DEL FILE GENERATO | ||
+ | La dimensione del file generato sara': | ||
+ | | ||
+ | nel caso del calcolatore su cui e' stato testato il programma: | ||
+ | 2*60 = 120 byte | ||
+ | */ | ||
+ | | ||
+ | fclose(fp); /* Chiusura del file */ | ||
+ | |||
+ | |||
+ | | ||
+ | fp = fopen(" | ||
+ | if (fp == NULL){ | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* Lettura del file */ | ||
+ | n_stud = 0; | ||
+ | while( fread(& | ||
+ | n_stud++; | ||
+ | } | ||
+ | |||
+ | fclose(fp); /* Chiusura del file */ | ||
+ | |||
+ | | ||
+ | /* Stampa dei record letti */ | ||
+ | for (i=0; i< | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/scrittura_e_lettura_file_binario?do=diff&rev2%5B0%5D=&rev2%5B1%5D=1551188125&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/it/informatica/linguaggio_c/scrittura_e_lettura_file_binario.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1