I'm trying to write a small database program which will have 5 functions, the first one is Add() but I get SegFault error on scanf:
void Add(); struct data{ char name[20]; char description[300]; int quantity; }; typedef struct data dataobj; dataobj element; int main() { Add(); return 0; } Add() { FILE *database; database = fopen("database.txt", "a+"); printf("Object: \n"); fgets(element.name,20,stdin); fprintf(database, element.name); printf("Description: \n"); fgets(element.description,300,stdin); fprintf(database, element.description); printf("Quantity: \n"); scanf("%d",&element.quantity); fprintf(database, element.quantity); fclose(database); }
this is the error: Program received signal SIGSEGV, Segmentation fault. In ungetwc () (C:\WINDOWS\SysWOW64\msvcrt.dll)
debugger window:
#0 0x77bea965 ungetwc() (C:\WINDOWS\SysWOW64\msvcrt.dll:??) #1 0x77c21268 msvcrt!_iob() (C:\WINDOWS\SysWOW64\msvcrt.dll:??) #2 ?? ?? () (??:??)
Also I noticed that if I write fgets after scanf instruction, fgets will not be executed for some reasons.. So, in the prototype I had to keep this order: char char int (for example I couldnt write: char int char)