#include #include #define NAME 50 #define COMMENT 1000 struct series{ char name[NAME]; int year; int season; char status[20]; int ep_cast; int ep_watch; float rating; char comment[COMMENT]; }; void add(FILE *); void set_status(struct series *); void modify(FILE *); void list(FILE *); void main(){ FILE *pf; int o; do{ printf("\n0: Add new serie\n1: List your series\n2: Modify data\n3: Exit \n"); scanf("%d",&o); switch(o){ case 0: add(pf); break; case 1: list(pf); break; case 2: modify(pf); break; default: printf("\nIncorrect Option"); break; } }while(o!=3); } void add(FILE *pf){ struct series new; char name[NAME+1], comment[COMMENT+1]; do { printf("Name: "); __fpurge(stdin); gets(name); }while(strlen(name)>NAME); strcpy( new.name, name ); do{ printf("Year: "); fflush(stdin); scanf("%d",&new.year); }while(new.year<0 || new.year > 2200); do{ printf("Season: "); fflush(stdin); scanf("%d",&new.season); }while(new.season<0 || new.season > 20); do{ printf("Cast Episodes: "); fflush(stdin); scanf("%d",&new.ep_cast); }while(new.ep_cast<0 || new.ep_cast > 2000); do{ printf("Watched Episodes: "); fflush(stdin); scanf("%d",&new.ep_watch); }while(new.ep_watch<0 || new.ep_watch > new.ep_cast); set_status(&new); do{ printf("Rating: "); fflush(stdin); scanf("%f",&new.rating); }while(new.rating<0 || new.rating > 10); do { printf("Comment: "); __fpurge(stdin); gets(comment); }while(strlen(comment)>NAME); strcpy(new.comment, comment); if((pf=fopen("data.bin","ab"))==NULL) printf("Can't open the file\n"); else{ fwrite(&new, sizeof(struct series), 1, pf); fclose(pf); } } void set_status(struct series *new){ int o; char watched[]="Watched"; char watching[]="Watching"; char stalled[]="Stalled"; char dropped[]="Dropped"; do{ printf("Status\n\t0: Watched\n\t1: Watching\n\t2: Stalled\n\t3: Dropped\n"); scanf("%d",&o); switch(o){ case 0: if(new->ep_watchep_watch) strcpy(new->status, watched); else strcpy(new->status, watching); break; case 1: strcpy(new->status, watched); break; case 2: strcpy(new->status, stalled); break; case 3: strcpy(new->status, dropped); break; default: printf("\nIncorrect Option"); break; } }while(o<0 || o>3); } void list(FILE *pf){ struct series new; int pos=0,lon,i; if((pf=fopen("data.bin","rb"))==NULL) printf("Can't open the file\n"); else{ fseek(pf,0,SEEK_END); lon=ftell(pf)/sizeof(struct series); fseek(pf,0,SEEK_SET); for(i=0;iNAME); strcpy( new.name, name ); break; case 1: do{ printf("Year: "); fflush(stdin); scanf("%d",&new.year); }while(new.year<0 || new.year > 2200); break; case 2: do{ printf("Season: "); fflush(stdin); scanf("%d",&new.season); }while(new.season<0 || new.season > 20); break; case 3: do{ printf("Cast Episodes: "); fflush(stdin); scanf("%d",&new.ep_cast); }while(new.ep_cast<0 || new.ep_cast > 2000); break; case 4: do{ printf("Watched Episodes: "); fflush(stdin); scanf("%d",&new.ep_watch); }while(new.ep_watch<0 || new.ep_watch > new.ep_cast); break; case 5: set_status(&new); break; case 6: do{ printf("Rating: "); fflush(stdin); scanf("%f",&new.rating); }while(new.rating<0 || new.rating > 10); break; case 7: do { printf("Comment: "); __fpurge(stdin); gets(comment); }while(strlen(comment)>NAME); strcpy(new.comment, comment); break; default: printf("\nIncorrect Option"); break; } }while(o!=8); fseek(pf,pos*sizeof(struct series), SEEK_SET); fwrite(&new, sizeof(struct series), 1, pf); fclose(pf); } }