From d6b707aa5cb246c5cd5320c5f7d1b52222ccbe3e Mon Sep 17 00:00:00 2001 From: medina5 Date: Sat, 11 Oct 2025 13:27:21 +0200 Subject: [PATCH] stdio --- fichiers.md | 8 ++-- stdio.md | 106 ++++++++++++++++++++++----------------------- td3.c => td3/td3.c | 0 td4.c => td4/td4.c | 1 - td5.md | 2 + td5/td5.c | 102 +++++++++++++++++++++++++++++++++++++++++++ td5/td5.h | 23 ++++++++++ 7 files changed, 185 insertions(+), 57 deletions(-) rename td3.c => td3/td3.c (100%) rename td4.c => td4/td4.c (99%) create mode 100644 td5/td5.c create mode 100644 td5/td5.h diff --git a/fichiers.md b/fichiers.md index a70581f..a08991c 100644 --- a/fichiers.md +++ b/fichiers.md @@ -6,6 +6,8 @@ En C, un fichier est une suite d'octets. Les informations contenues dans un fich Le positionnement dans un fichier est donnée par un pointeur. +La bibliothèque [stdio](stdio) + ### Types de fichier Les **binaires** : Dans un fichier dit "binaire", les informations sont codées telles que, comme en mémoire. Ce sont généralement des nombres. Ils ne sont pas lisibles sauf avec un éditeur hexadécimal ou le programme qui l'a écrit. @@ -23,9 +25,9 @@ typdef struct { char *buffer; // pointeur vers le tampon char *ptr; // pointeur vers le caractère suivant dans le tampon - int cnt; // nombre de caractères dans le tampon - int flag; // bits donnant l'état du fichier - int fd; // descripteur (identifiant de fichier) + int cnt; // nombre de caractères dans le tampon + int flag; // bits donnant l'état du fichier + int fd; // descripteur (identifiant de fichier) } FILE; ``` diff --git a/stdio.md b/stdio.md index 7c34a20..2562ae6 100644 --- a/stdio.md +++ b/stdio.md @@ -1,63 +1,63 @@ ---- -title: stdio ---- +# Bibliothèque stdio -fonction|macro|description ----|--- -gets|Lit une chaine depuis le clavier -puts|Écrit une chaine à l'écran -fgets|Lit une chaine depuis un fichier -fputs|Écrit une chaine dans un fichier +Standard Input/Output + +fonction |description +--- |--- +gets |Lit une chaine depuis le clavier +puts |Écrit une chaine à l'écran +fgets |Lit une chaine depuis un fichier +fputs |Écrit une chaine dans un fichier -fonction|description ----|--- -getc|Get character from stream -putc|Write character to stream -fgetc|Get character from stream -fputc|Write character to stream -getchar|Get character from stdin -putchar|Write character to stdout -ungetc|Unget character from stream +fonction |description +--- |--- +getc |Get character from stream +putc |Write character to stream +fgetc |Get character from stream +fputc |Write character to stream +getchar |Get character from stdin +putchar |Write character to stdout +ungetc |Unget character from stream -fonction|description ----|--- -fprintf|Write formatted data to stream -fscanf|Read formatted data from stream -printf|Print formatted data to stdout -scanf|Read formatted data from stdin -snprintf|Write formatted output to sized buffer -sprintf|Write formatted data to string -sscanf|Read formatted data from string -vfprintf|Write formatted data from variable argument list to stream -vfscanf|Read formatted data from stream into variable argument list -vprintf|Print formatted data from variable argument list to stdout -vscanf|Read formatted data into variable argument list -vsnprintf|Write formatted data from variable argument list to sized buffer -vsprintf|Write formatted data from variable argument list to string -vsscaf|Read formatted data from string into variable argument list +fonction |description +--- |--- +fprintf |Write formatted data to stream +fscanf |Read formatted data from stream +printf |Print formatted data to stdout +scanf |Read formatted data from stdin +snprintf |Write formatted output to sized buffer +sprintf |Write formatted data to string +sscanf |Read formatted data from string +vfprintf |Write formatted data from variable argument list to stream +vfscanf |Read formatted data from stream into variable argument list +vprintf |Print formatted data from variable argument list to stdout +vscanf |Read formatted data into variable argument list +vsnprintf |Write formatted data from variable argument list to sized buffer +vsprintf |Write formatted data from variable argument list to string +vsscaf |Read formatted data from string into variable argument list -fonction|description ----|--- -fread|Read block of data from stream -fwrite|Write block of data to stream +fonction |description +--- |--- +fread |Read block of data from stream +fwrite |Write block of data to stream -fonction|description ----|--- -fclose|Close file -fflush|Flush stream -fopen|Open file -freopen|Reopen stream with different file or mode -setbuf|Set stream buffer -setvbufChange stream buffering +fonction |description +--- |--- +fclose |Close file +fflush |Flush stream +fopen |Open file +freopen |Reopen stream with different file or mode +setbuf |Set stream buffer +setvbuf |Change stream buffering -fonction|description ----|--- -fgetpos|Get current position in stream -fseek|Reposition stream position indicator -fsetpos|Set position indicator of stream -ftell|Get current position in stream -rewind|St position of stream to the beginning +fonction |description +--- |--- +fgetpos |Get current position in stream +fseek |Reposition stream position indicator +fsetpos |Set position indicator of stream +ftell |Get current position in stream +rewind |St position of stream to the beginning clearerr|Clear error indicators feof|Check end-of-file indicator diff --git a/td3.c b/td3/td3.c similarity index 100% rename from td3.c rename to td3/td3.c diff --git a/td4.c b/td4/td4.c similarity index 99% rename from td4.c rename to td4/td4.c index 5e3f7a9..e204444 100644 --- a/td4.c +++ b/td4/td4.c @@ -11,7 +11,6 @@ int main() { FILE *fC = fopen("C.csv"); FILE *fD = fopen("D.csv"); - float vA, vB, vC, vD; while (fscanf(fichier, "%f", &vA, &vB, &vC, &vD)==4) { printf("%f\n", valeur); diff --git a/td5.md b/td5.md index 3572da1..cee1e95 100644 --- a/td5.md +++ b/td5.md @@ -26,3 +26,5 @@ Proposer une structure qui permet de stocker les information du fichier tsv au f Nb Ajouter plus de ligne pour voir la diff entre float et int float plus de capacité mais moins précis +Structure pour stat +Fonctions avec pointeur diff --git a/td5/td5.c b/td5/td5.c new file mode 100644 index 0000000..4c18fa1 --- /dev/null +++ b/td5/td5.c @@ -0,0 +1,102 @@ + +#include // < > bibliothèue du système +#include "main.h" // " " fichiers du projet + +int main() { + + FILE *fichier = fopen("production.tsv", "r"); + if (fichier == NULL) { + perror("Erreur d'ouverture du fichier"); + return 1; + } + + // Lecture de la première ligne + char entete[256]; + fgets(entete, sizeof(entete), fichier); + // Lecture de la ligne complète ou 256 caractères max + // Attention ! fichier à la fin contrairement à fscanf + + char designation[20]; // Le minimum 13 + 1 + int hauteur, largeur, epaisseur; + float temps; + // Au total les variables sont déclarée en dehors et avant la boucle + int perimetreT = 0; + int volumeT = 0; + + Stats statVL; + Stats statVV; + + statVL.max = 99; + printf("STATVL MAX %f\n", statVL.max); + StatInitialisation(&statVL); // & adresse du pointeur Stats * + printf("STATVL MAX %f\n", statVL.max); + + StatInitialisation(&statVV); + + int compteur = 0; + + // le tableau de char est DEJA un pointeur pas de & avec designation + while (fscanf(fichier,"%s %d %d %d %f", designation, &largeur, &hauteur, &epaisseur, &temps) == 5) { + printf("%s\n", designation); + + // A la ligne les variables sont déclarée dans la boucle + int perimetre = (largeur + hauteur) * 2; + int volume = largeur * hauteur * epaisseur; + + float vl = perimetre /temps; // vitesse linéaire + float vv = volume / temps; // vitesse volumique + + StatAccumulation(&statVL, vl); + StatAccumulation(&statVV, vv); + + perimetreT += perimetre; + volumeT += volume; + compteur++; + } + + StatFinal(&statVL, compteur); + StatFinal(&statVV, compteur); + + printf("Périmètre Total %d\n", perimetreT); //236 600 + printf("Volume Total %d\n", volumeT); // 773 993 000 + + printf("VL min %f max %f\n", statVL.min, statVL.max); + // 48.2 52.0 + printf("VL moyenne %f variance %f\n", statVL.moyenne, statVL.variance); + // 50.37 1.19 + printf("VV min %f max %f\n", statVV.min, statVV.max); + // 12 757 339 456 + printf("VV moyenne %f variance %f\n", statVV.moyenne, statVV.variance); + // 142 226 9056849920 +} + +void StatInitialisation(Stats *s) { // * pointeur + s->max = 0.0; // *. -> + s->min = __FLT_MAX__; + s->somme = 0.0; + s->somme2 = 0.0; + s->moyenne = s->moyenne2 = s->variance = 0.0; +} // Avec un pointeur les modifications sur la variables d'entrée + // sont répercutées sur l'original (StatVL ou StatVV) + +void StatAccumulation(Stats *s, float valeur) { + + // Le minimum + if (valeur < s->min) { + s->min = valeur; + } + + // Le maximum + if (valeur > s->max) { + s->max = valeur; + } + + s->somme += valeur; + s->somme2 += valeur * valeur; +} + +void StatFinal(Stats *s, int compteur) { + s->moyenne = s->somme / compteur; + s->moyenne2 = s->moyenne * s->moyenne; + s->variance = s->somme2 / compteur - s->moyenne2; +} diff --git a/td5/td5.h b/td5/td5.h new file mode 100644 index 0000000..f35754e --- /dev/null +++ b/td5/td5.h @@ -0,0 +1,23 @@ +typedef struct { + float min; + float max; + float somme; + float somme2; + float moyenne; + float moyenne2; + float variance; +} Stats; + +typedef struct { + char designation[20]; + int hauteur, largeur, epaisseur; + float temps; + int perimetre; + int volume; +} Valeurs; + +void StatInitialisation(Stats *s); // Prototype de fonction +// Pas de code { } juste un ; +// Se trouve généralement dans un fichier .h +void StatAccumulation(Stats *s, float valeur); +void StatFinal(Stats *s, int compteur);