0

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;}
fazan
  • 155
  • 1
  • 3
  • 12
  • Please, properly format your code. Use some indentation and expand your one-line `if` conditions for legibility. – delirium Dec 21 '18 at 12:10
  • have you tried fflush(stdin) instead of fflush (NULL) ? – pm101 Dec 21 '18 at 12:12
  • @delirium those if conditions just print error messages and aren't important – fazan Dec 21 '18 at 12:14
  • @pm101 it seems fflush() isn't defined on input streams on my machine – fazan Dec 21 '18 at 12:15
  • 1
    @pm101: `fflush` is defined by the C standard only for flushing output and update streams. Microsoft extended it for input streams, but such use should not be recommended generally. – Eric Postpischil Dec 21 '18 at 12:16
  • Although I do remember this worked fine back in the day or Borland Turbo C xD... of course I have forgotten a lot since then – pm101 Dec 21 '18 at 12:26

0 Answers0