1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
char line[1000],keyword[50];
enum Keyword {TEMP,PRES,EGR} kwd;
int nbkeywords=3; /* nombre de mot cle == "longueur" de Keyword */
char * listkwd={"TEMP","PRES","EGR"}; /* liste des mots-cle */
int keywordOK[3]={0}; /* keywordOK[i]==1 : le mot-cle a ete saisi
keywordOK[i]==0 : le mot-cle n'a pas ete saisi
*/
double temp,pres,egr;
int err;
char * p=NULL;
FILE * file=fopen("input.txt","r");
if(file==NULL)
{
fprintf(stderr,"erreur ouverture\n");
exit(1);
}
while(fgets (line, sizeof line, file) != NULL)
{
p=strchr(line,'\n');
if(p!=NULL) *p=NULL;
if(strstr(line,"TEMP ")!=NULL)
{
sscanf(line,"%s%lf",keyword,&temp);
kwd=TEMP;
keywordOK[kwd]=1;
}
else if(strstr(line,"PRES ")!=NULL)
{
sscanf(line,"%s%lf",keyword,&pres);
kwd=PRES;
keywordOK[kwd]=1;
}
else
{
sscanf(line,"%s%lf",keyword,&egr);
kwd=EGR;
keywordOK[kwd]=1;
}
} /* fin while(fgets (line, sizeof line, file) != NULL) */
if(!feof(file))
{
fprintf(stderr,"%s %d : Error : a misreading occurred in file input.txt\n",__FILE__,__LINE__);
return EXIT_FAILURE;
}
if(ferror(file))
{
fprintf(stderr,"%s %d : a misreading occurred in file input.txt\n",__FILE__,__LINE__);
return EXIT_FAILURE;
}
fclose(file); file=NULL;
unsigned i;
err=0;
for(i=0;i<nbkeywords;++i)
{
if(keywordOK[i]==0)
{
fprintf(stderr,"keyword %s must be specified\n",listkwd[i]);
err=1;
}
}
if(err==1) exit(1);
printf("T = %f P = %f EGR = %f\n",temp,pres,egr);
return 0;
} |
Partager