0

I'm managing in WM_COMMAND this case where once the menu AppendMenu(hMenu,MF_POPUP (UINT_PTR)IMPORT, "import"); is pressed it will import all the data and store it in each of the variables.

I do have my structures into a header file which is called struct.h which contains:

typedef struct userglobal{
    char name[15];
    int  born;
    float price;
    float average;
}userglobal;

and here's the window procedure:

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp){
    switch(msg){
        FILE *sorgente;
        int i = 0;
        case WM_COMMAND:
            switch(wp){
                case UTENTE_OPEN:
                    MessageBeep(MB_OK);
                    break;
                case RISTORANTE_OPEN:
                    MessageBeep(MB_ICONHAND);
                    break;
                case UTENTE_MODIFICA:
                   MessageBeep(MB_ICONHAND);
                   break;
                case 2:
                        MessageBox(NULL,"programma che ti permette di prenotare posti online...(da continuare)","Come Funziona?", MB_OK);
                        break;
                case IMPORT:
                    if ((source = fopen("./Data/users.csv", "r")) == NULL)
                    printf("can't open the file.\n");
                    else{
                        while(!feof(source)){
                        fscanf(source,"%[^,],%d,%f,%f\n", user[i].name, &user[i].born, &user[i].price, &user[i].average); 
                        i++;
                        }
                    }
                    fclose(source);
                    break;
    case WM_CREATE:
        AddMenus(hWnd);
        //AddControls(hWnd);
        break;
    case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default:
             return DefWindowProcW(hWnd,msg,wp,lp);
    }
}

I declare userglobal user[1000]; user as global scope.

When I press the import menu everything behind fscanf in Code::Blocks will be executed while in VSCode the program will not even enter the while condition which is kind of weird. with gdb I get:

Program received signal SIGSEGV, Segmentation fault.
0x776ef391 in ungetwc ()

but I have no clue why should I get a segmentation fault.

EDIT: I've added more code into it and fixed some mistyped elements

EDIT: Fixed code:

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp){
    switch(msg){
        FILE *sorgente;
        case WM_COMMAND:
            switch(wp){
                case UTENTE_OPEN:
                    MessageBeep(MB_OK);
                    break;
                case RISTORANTE_OPEN:
                    MessageBeep(MB_ICONHAND);
                    break;
                case UTENTE_MODIFICA:
                   MessageBeep(MB_ICONHAND);
                   break;
                case 2:
                        MessageBox(NULL,"programma che ti permette di prenotare posti online...(da continuare)","Come Funziona?", MB_OK);
                        break;
                case IMPORT:{
                    int i = 0;
                    if ((source = fopen("./Data/users.csv", "r")) == NULL)
                    printf("can't open the file.\n");
                    else{
                        while(!feof(source)){
                        fscanf(source,"%[^,],%d,%f,%f\n", user[i].name, &user[i].born, &user[i].price, &user[i].average); 
                        i++;
                        }
                     }
                    fclose(source);
                    }
                    break;
    case WM_CREATE:
        AddMenus(hWnd);
        //AddControls(hWnd);
        break;
    case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default:
             return DefWindowProcW(hWnd,msg,wp,lp);
    }
}

  • That is way too little code to tell much about a reason. Just as a guess, what is `i`? Is it initialized properly? Also you might want to take a look at [Why is while(!feof(file)) always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Gerhardh May 22 '21 at 13:41
  • If `user` is an array of struct, how would `user->name != ""` ever compile? And anyway that is not how strings are compared. You must use `strcmp` to compare strings. Otherwise you compare the address of that array with the address of that empty string literal which can never be the same. – Gerhardh May 22 '21 at 13:43
  • If you define `userglobal user[1000];` you must use a C++ compiler, don't you? In C there is no type `userglobal` with the struct declaration you show us. In C you must use `struct userglobal` or `utente`. – Gerhardh May 22 '21 at 13:47
  • i've changed some of the things – diane fink May 22 '21 at 14:06
  • `i` is uninitialized. Move the definition before the `switch` – Gerhardh May 22 '21 at 14:15
  • i cant declare i in the import case, the compiler gives me an error which is `a label can only be part of a statement and a declaration is not a statement` – diane fink May 22 '21 at 14:16
  • The compiler is right. You would need to add a new block `{ ... }` to do that: `switch IMPORT: {int i=0; ... } break;` or move it into the `else` block. Or move it before the `switch` as I suggested. – Gerhardh May 22 '21 at 14:18
  • You should remove that uninitialized variable `i`. Just in case you might use it in another case later and run into same trap agian. – Gerhardh May 22 '21 at 15:23

0 Answers0