Codice: Seleziona tutto
void create_coursefile()
{
system("clear");
FILE *f;
char nameFile[20];
char nameIndex[20];
char nameIndexCode[20];
printf("Nome del file corso da creare: ");
scanf("%s", nameFile);
struct Course c;
strcpy(c.name_course,"VUOTO");
strcpy(c.name_dir,"VUOTO");
strcpy(c.surn_dir,"VUOTO");
c.id = -1;
c.deleted = 1;
strcpy(nameIndex,nameFile);
strcat(nameIndex,".txt"); //.txt
strcpy(nameIndexCode,nameIndex);
strcat(nameIndexCode,"c");//.txtc
strcat(nameFile, ".dat");
f = fopen(nameFile,"rb");
if(f == NULL) //se il file non esiste
{
fclose(f);
f = fopen(nameFile,"wb");
int i;
for(i=0;i<max;i++)
{
fwrite(&c,sizeof(struct Course),1,f);
}
fclose(f);
printf("File scritto correttamente!\n");
FILE *indice;
struct Index_course x;
strcpy(x.key,"VUOTO");
indice = fopen(nameIndex,"wb");
for(i=0;i<max;i++)
{
x.value = i;
fwrite(&x,sizeof(struct Index_course),1,indice);
}
fclose(indice);
printf("File indice del corso scritto correttamente\n");
indice = fopen(nameIndexCode,"wb");
struct IndexCode_course y;
y.key = -1;
y.value = -1;
for(i=0;i<max;i++)
{
fwrite(&y,sizeof(struct IndexCode_course),1,indice);
}
fclose(indice);
}
else
{
char s;
printf("Il file esiste gia'. Lo vuoi sovrascivere? (s/n): ");
scanf("%s", &s);
if(s == 's' || s == 'S')
{
f = fopen(nameFile,"wb");
int i;
for(i=0;i<max;i++)
{
fwrite(&c,sizeof(struct Course),1,f);
}
fclose(f);
printf("File sovrascritto correttamente!\n");
FILE *indice;
struct Index_course x;
strcpy(x.key,"VUOTO");
indice = fopen(nameIndex,"wb");
for(i=0;i<max;i++)
{
x.value = i;
fwrite(&x,sizeof(struct Index_course),1,indice);
}
fclose(indice);
printf("File indice del corso scritto correttamente\n");
indice = fopen(nameIndexCode,"wb");
struct IndexCode_course y;
y.key = -1;
y.value = -1;
for(i=0;i<max;i++)
{
fwrite(&y,sizeof(struct IndexCode_course),1,indice);
}
fclose(indice);
}
}
printf("Operazione completata\n");
}