stdio

2025-10-11 13:27:21 +02:00
parent fa95ef7348
commit d6b707aa5c
7 changed files with 185 additions and 57 deletions

@@ -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.

106
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

@@ -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);

2
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

102
td5/td5.c Normal file

@@ -0,0 +1,102 @@
#include <stdio.h> // < > 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;
}

23
td5/td5.h Normal file

@@ -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);