stdio
@@ -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.
|
Le positionnement dans un fichier est donnée par un pointeur.
|
||||||
|
|
||||||
|
La bibliothèque [stdio](stdio)
|
||||||
|
|
||||||
### Types de fichier
|
### 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.
|
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
106
stdio.md
@@ -1,63 +1,63 @@
|
|||||||
---
|
# Bibliothèque stdio
|
||||||
title: stdio
|
|
||||||
---
|
|
||||||
|
|
||||||
fonction|macro|description
|
Standard Input/Output
|
||||||
---|---
|
|
||||||
gets|Lit une chaine depuis le clavier
|
fonction |description
|
||||||
puts|Écrit une chaine à l'écran
|
--- |---
|
||||||
fgets|Lit une chaine depuis un fichier
|
gets |Lit une chaine depuis le clavier
|
||||||
fputs|Écrit une chaine dans un fichier
|
puts |Écrit une chaine à l'écran
|
||||||
|
fgets |Lit une chaine depuis un fichier
|
||||||
|
fputs |Écrit une chaine dans un fichier
|
||||||
|
|
||||||
|
|
||||||
fonction|description
|
fonction |description
|
||||||
---|---
|
--- |---
|
||||||
getc|Get character from stream
|
getc |Get character from stream
|
||||||
putc|Write character to stream
|
putc |Write character to stream
|
||||||
fgetc|Get character from stream
|
fgetc |Get character from stream
|
||||||
fputc|Write character to stream
|
fputc |Write character to stream
|
||||||
getchar|Get character from stdin
|
getchar |Get character from stdin
|
||||||
putchar|Write character to stdout
|
putchar |Write character to stdout
|
||||||
ungetc|Unget character from stream
|
ungetc |Unget character from stream
|
||||||
|
|
||||||
fonction|description
|
fonction |description
|
||||||
---|---
|
--- |---
|
||||||
fprintf|Write formatted data to stream
|
fprintf |Write formatted data to stream
|
||||||
fscanf|Read formatted data from stream
|
fscanf |Read formatted data from stream
|
||||||
printf|Print formatted data to stdout
|
printf |Print formatted data to stdout
|
||||||
scanf|Read formatted data from stdin
|
scanf |Read formatted data from stdin
|
||||||
snprintf|Write formatted output to sized buffer
|
snprintf |Write formatted output to sized buffer
|
||||||
sprintf|Write formatted data to string
|
sprintf |Write formatted data to string
|
||||||
sscanf|Read formatted data from string
|
sscanf |Read formatted data from string
|
||||||
vfprintf|Write formatted data from variable argument list to stream
|
vfprintf |Write formatted data from variable argument list to stream
|
||||||
vfscanf|Read formatted data from stream into variable argument list
|
vfscanf |Read formatted data from stream into variable argument list
|
||||||
vprintf|Print formatted data from variable argument list to stdout
|
vprintf |Print formatted data from variable argument list to stdout
|
||||||
vscanf|Read formatted data into variable argument list
|
vscanf |Read formatted data into variable argument list
|
||||||
vsnprintf|Write formatted data from variable argument list to sized buffer
|
vsnprintf |Write formatted data from variable argument list to sized buffer
|
||||||
vsprintf|Write formatted data from variable argument list to string
|
vsprintf |Write formatted data from variable argument list to string
|
||||||
vsscaf|Read formatted data from string into variable argument list
|
vsscaf |Read formatted data from string into variable argument list
|
||||||
|
|
||||||
fonction|description
|
fonction |description
|
||||||
---|---
|
--- |---
|
||||||
fread|Read block of data from stream
|
fread |Read block of data from stream
|
||||||
fwrite|Write block of data to stream
|
fwrite |Write block of data to stream
|
||||||
|
|
||||||
fonction|description
|
fonction |description
|
||||||
---|---
|
--- |---
|
||||||
fclose|Close file
|
fclose |Close file
|
||||||
fflush|Flush stream
|
fflush |Flush stream
|
||||||
fopen|Open file
|
fopen |Open file
|
||||||
freopen|Reopen stream with different file or mode
|
freopen |Reopen stream with different file or mode
|
||||||
setbuf|Set stream buffer
|
setbuf |Set stream buffer
|
||||||
setvbufChange stream buffering
|
setvbuf |Change stream buffering
|
||||||
|
|
||||||
fonction|description
|
fonction |description
|
||||||
---|---
|
--- |---
|
||||||
fgetpos|Get current position in stream
|
fgetpos |Get current position in stream
|
||||||
fseek|Reposition stream position indicator
|
fseek |Reposition stream position indicator
|
||||||
fsetpos|Set position indicator of stream
|
fsetpos |Set position indicator of stream
|
||||||
ftell|Get current position in stream
|
ftell |Get current position in stream
|
||||||
rewind|St position of stream to the beginning
|
rewind |St position of stream to the beginning
|
||||||
|
|
||||||
clearerr|Clear error indicators
|
clearerr|Clear error indicators
|
||||||
feof|Check end-of-file indicator
|
feof|Check end-of-file indicator
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ int main() {
|
|||||||
FILE *fC = fopen("C.csv");
|
FILE *fC = fopen("C.csv");
|
||||||
FILE *fD = fopen("D.csv");
|
FILE *fD = fopen("D.csv");
|
||||||
|
|
||||||
|
|
||||||
float vA, vB, vC, vD;
|
float vA, vB, vC, vD;
|
||||||
while (fscanf(fichier, "%f", &vA, &vB, &vC, &vD)==4) {
|
while (fscanf(fichier, "%f", &vA, &vB, &vC, &vD)==4) {
|
||||||
printf("%f\n", valeur);
|
printf("%f\n", valeur);
|
||||||
2
td5.md
2
td5.md
@@ -26,3 +26,5 @@ Proposer une structure qui permet de stocker les information du fichier tsv au f
|
|||||||
Nb
|
Nb
|
||||||
Ajouter plus de ligne pour voir la diff entre float et int
|
Ajouter plus de ligne pour voir la diff entre float et int
|
||||||
float plus de capacité mais moins précis
|
float plus de capacité mais moins précis
|
||||||
|
Structure pour stat
|
||||||
|
Fonctions avec pointeur
|
||||||
|
|||||||
102
td5/td5.c
Normal file
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
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);
|
||||||
Reference in New Issue
Block a user