Prova cosi:
***************************************************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE* openfile(char* filename,const char* mode);
void closefile(FILE* fp);
int readfile_info(FILE* fp, struct info* albergo);
void readfile_prezzo(FILE* fp,FILE* out,struct info* albergo, int alb_len);
void writefile_info(FILE* fp,struct info* albergo);
void writefile_oldinfo(FILE* fp,struct info* albergo, int alb_len);
int get_range_alberghi(float max_val,float min_val,struct info* albergo, int alb_len);
struct info
{
char codice[256];
char nome[256];
int categoria;
int camere;
float prezzo;
};
int main()
{
struct info albergo[10]={0};
FILE* informazioni = openfile("C:\\info.txt","r");
FILE* prezzo = openfile("C:\\prezzi.txt","r");
FILE* oinfo = openfile("C:\\oldinfo.txt","w");
FILE* ninfo = openfile("C:\\newinfo.txt","w");
int len = readfile_info(informazioni,albergo);
writefile_oldinfo(oinfo,albergo,len);
readfile_prezzo(prezzo,ninfo,albergo,len);
int N_range_albergi = get_range_alberghi(50.2,30.4,albergo,len);
closefile(informazioni);
closefile(prezzo);
closefile(oinfo);
closefile(ninfo);
system("pause");
}
FILE* openfile(char* filename,const char* mode)
{
FILE* fp;
fp = fopen(filename,mode);
return fp;
}
void closefile(FILE* fp)
{
fclose(fp);
}
int readfile_info(FILE* fp, struct info* albergo)
{
int i;
for(i=0;!feof(fp) ;i++)
fscanf(fp,"%s%s%d%d%f",albergo.codice,albergo.nome,&albergo.categoria,&albergo.camere,&albergo.prezzo);
return i;
}
void readfile_prezzo(FILE* fp,FILE* out,struct info* albergo, int alb_len)
{
int i;
bool find;
char codice[256];
float prezzo;
while(!feof(fp))
{
fscanf(fp,"%s",codice);
for(i=0, find=false;i<alb_len;i++)
{
if(strcmp(codice,albergo.codice)==0)
{
writefile_info(out,&albergo);
fscanf(fp,"%f",&prezzo);
albergo.prezzo = prezzo;
writefile_info(out,&albergo);
find=true;
}
}
if(find==false)
printf("Nel file prezzo.txt il codice %s non esiste",codice);
}
}
void writefile_info(FILE* fp,struct info* albergo)
{
fprintf(fp,"%s %s %d %d %3.2f\n",albergo->codice,albergo->nome,albergo->categoria,albergo->camere,albergo->prezzo);
}
void writefile_oldinfo(FILE* fp,struct info* albergo, int alb_len)
{
for(int i=0;i<alb_len;i++)
fprintf(fp,"%s %s %d %d %3.2f\n",albergo.codice,albergo[i].nome,albergo[i].categoria,albergo[i].camere,albergo[i].prezzo);
}
int get_range_alberghi(float max_val,float min_val,struct info* albergo, int alb_len)
{
FILE* fp = openfile("C:\\elenco.txt","w");
int num=0;
for(int i=0;i<alb_len;i++)
{
if(albergo[i].prezzo > min_val && albergo[i].prezzo < max_val)
{
writefile_info(fp,&albergo[i]);
num++;
}
}
closefile(fp);
return ++num;
}
************************************************************************************************