I'm writing up a primitive database that stores, reads and changes data that consists of a max 31 length string and a double. I'm having problems with error handling when the user inputs a string longer than 31 characters. My program recognizes the error, prints out an error message and skips the operation, but continues to read from the mess that the user inputted. This is problematic because commands are given to the program via different characters at the beginning (I'm not allowed to change this because it's a homework problem).
I've tried using fflush(), but that obviously doesn't work, and using gets() to read the rest of the input and discard it, but my compiler refuses this function, I guess because it's unsafe.
How should I handle this properly without just aborting the program?
The rest of the code works as expected.
Here's my code for reference:
#include <stdio.h>
#include <assert.h>
#include <time.h>
typedef struct { //data in my database is in this type
char naziv[32];
double vrijednost;
} record;
int main(){
char o;
unsigned long j=0;
record r;
int dulj=sizeof(r);
//char glup[1024];
FILE *data;
FILE *log;
char ime[15];
time_t rawtime;
struct tm *info;
time( &rawtime );
info = localtime( &rawtime );
strftime (ime, 13, "log_%d%m%Y.txt",info);
data=fopen("data.bin","ab"); assert(data!=NULL);
assert(fclose(data)==0);
data=fopen("data.bin","rb+"); assert(data!=NULL);
log=fopen(ime,"a"); assert(log!=NULL);
if(fseek(data, 0, SEEK_END)!=0){printf("fatalna greška\n");fprintf(log,"\t-greška\n");fflush(NULL);fclose(data); fclose(log);return 0;}
printf("naredbe:\na\tucita input i upise u datoteku\nr\tucita broj ispise vrijednost na tpj poziciji\nw\tucita broj i input i upise na tu poziciju\nc\tzatvori program\n\n");
while(1){
scanf(" %c",&o);
if(o=='a'){
if(fseek(data, 0, SEEK_END)!=0){printf("fatalna greška\n");fprintf(log,"\t-greška\n");fflush(NULL);fclose(data); fclose(log);return 0;}
if(scanf(" %31[^,], %lf", r.naziv, &r.vrijednost)!=2){printf("greška pri unosu\n");gets();continue;}
fprintf(log,"a %s\t%lf\n",r.naziv,r.vrijednost);
fflush(NULL);
if(fwrite(&r,dulj,1,data)!=1){printf("greška pri zapisivanju\n"); fprintf(log,"\t-greška\n");fflush(NULL);continue;}
continue;
}
else if (o=='r'){
scanf(" %lu",&j);
fprintf(log,"r %lu\n",j);
fflush(NULL);
if(fseek(data,j*dulj,SEEK_SET)!=0){printf("greška pri traženju pozicije\n");fprintf(log,"\t-greška\n");fflush(NULL);if(fseek(data, 0, SEEK_END)!=0){printf("fatalna greška\n");fprintf(log,"\t-greška\n");fflush(NULL);fclose(data); fclose(log);return 0;} continue;}
if(fread(&r,dulj,1,data)!=1){printf("greška pri čitanju\n");fprintf(log,"\t-greška\n");fflush(NULL);continue;}
printf("%s %lf\n", r.naziv, r.vrijednost);
if(fseek(data, 0, SEEK_END)!=0){printf("fatalna greška\n");fprintf(log,"\t-greška\n");fflush(NULL);fclose(data); fclose(log);return 0;}
continue;
}
else if (o=='w'){
scanf(" %lu",&j);
if(scanf(" %31[^,], %lf", r.naziv, &r.vrijednost)!=2){printf("greška pri unosu\n");gets();continue;}
fprintf(log,"w %lu %s\t%lf\n",j,r.naziv,r.vrijednost);
fflush(NULL);
if(fseek(data,j*dulj,SEEK_SET)!=0){printf("greška pri traženju pozicije\n");fprintf(log,"\t-greska\n");fflush(NULL);continue;}
if(fwrite(&r,dulj,1,data)!=1){printf("greška pri zapisivanju\n"); fprintf(log,"\t-greska\n");fflush(NULL);continue;}
if(fseek(data, 0, SEEK_END)!=0){printf("fatalna greška\n");fprintf(log,"\t-greška\n");fflush(NULL);fclose(data); fclose(log);return 0;}
continue;
}
else if (o=='c'){fprintf(log,"c\n");fflush(NULL); fclose(data); fclose(log); return 0;}
else printf("nepoznata naredba\n");
}
return 0;}