خرید بک لینک

Vote count: 0

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();

        retu 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:WINDOWSSysWOW64msvcrt.dll)

debugger window:


    #0 0x77bea965   ungetwc() (C:WINDOWSSysWOW64msvcrt.dll:??)
    #1 0x77c21268   msvcrt!_iob() (C:WINDOWSSysWOW64msvcrt.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)

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 1 تير 1395 ساعت: 3:53

صفحه بندی