-
crashing program, help
hey guys, my program is crashing, can someone see why
try looking in case open:
Code:
#include <windows.h>
#include <stdio.h>
#define ID_EDIT 1
#define ID_BUTTON 2
#define ID_STATIC 3
#define ID_LISTBOX 4
#define ID_NEW 1000
#define ID_OPEN 1004
#define ID_CLOSE 1001
#define ID_EXIT 1002
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
FILE *pfile;
FILE *pfile2;
static HWND hwndEdit;
static HWND hwndEdit2;
static HWND hwndEdit3;
static HWND hwndEdit4;
static HWND hwndButton;
static HWND hwndStatic;
static HWND hwndStatic2;
static HWND hwndStatic3;
static HWND hwndStatic4;
static HWND hwndStatic5;
static HWND hwndStatic6;
static HWND hwndlistbox;
static int len;
static CHAR text[1000];
static CHAR text2[1000];
static CHAR text3[1000];
static CHAR text4[1000];
int num1 = 0;
CREATESTRUCT *cs;
HMENU hMenu, hSubMenu, hSubMenu2;
char szText[64];
// Menu-item identifiers
static char szMenuItem[6][10] = {"Open", "Close", "Exit", "New"};
switch(msg)
{
case WM_CREATE:
/*hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
50, 70, 170, 140, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndEdit3 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
120, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndEdit4 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
190, 30, 40, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndStatic = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
1, 1, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic2 = CreateWindow(TEXT("static"), "/", WS_CHILD | WS_VISIBLE,
1, 5, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic3 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
1, 3, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic4 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
1, 4, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic5 = CreateWindow(TEXT("static"), "", WS_CHILD | WS_VISIBLE,
20, 220, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndButton = CreateWindow(
TEXT("button"), TEXT("set title and write to file"),
WS_VISIBLE | WS_CHILD,
30, 240, 200, 25,
hwnd, (HMENU) ID_BUTTON, NULL, NULL);*/
cs = (LPCREATESTRUCT)lParam;
hMenu = CreateMenu();
SetMenu(hwnd, hMenu);
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_OPEN, "&Open");
AppendMenu(hSubMenu, MF_STRING, ID_NEW, "&New");
AppendMenu(hSubMenu, MF_STRING, ID_EXIT, "&Exit");
InsertMenu(hMenu, 0, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu, "File");
DrawMenuBar(hwnd);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_OPEN :
DestroyWindow(hwndEdit);
DestroyWindow(hwndEdit2);
DestroyWindow(hwndEdit3);
DestroyWindow(hwndEdit4);
DestroyWindow(hwndStatic);
DestroyWindow(hwndStatic2);
DestroyWindow(hwndStatic3);
DestroyWindow(hwndStatic4);
DestroyWindow(hwndStatic5);
DestroyWindow(hwndButton);
hwndlistbox = CreateWindow(TEXT("Listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL,
0, 0, 265, 305, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
char *buff1;
char *character;
pfile = fopen("C:\\reminder\\data\\backup1.xls", "r");
while(fgets(buff1,50, pfile))
{
buff1 = character;
while(character != "\n"){
if(num1 != 3){
if(character == ","){
SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)" ");
num1 = num1 + 1 ;
}
else{
SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)character);
}
}
else{
printf("%s",character);
}
}
}
/*while(fgets(buff1,50, pfile))
{
SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)buff1);
}*/
return 0;
break;
case ID_NEW:
if(hwndEdit == NULL){
DestroyWindow(hwndlistbox);}
hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
50, 70, 170, 140, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndEdit3 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
120, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndEdit4 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
180, 30, 40, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndStatic = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
1, 1, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic2 = CreateWindow(TEXT("static"), "/", WS_CHILD | WS_VISIBLE,
1, 5, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic3 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
1, 3, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic4 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
1, 4, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndStatic5 = CreateWindow(TEXT("static"), "", WS_CHILD | WS_VISIBLE,
20, 220, 300, 20, hwnd, (HMENU) ID_STATIC,
NULL, NULL);
hwndButton = CreateWindow(
TEXT("button"), TEXT("set title and write to file"),
WS_VISIBLE | WS_CHILD,
30, 240, 200, 25,
hwnd, (HMENU) ID_BUTTON, NULL, NULL);
break;
case ID_EXIT :
if(MessageBox(hwnd,"are you sure you want to exit","Exit",MB_YESNO) == IDYES){
DestroyWindow(hwnd);}
else{
return 0;
}
break;
/*sprintf(szText, "You chose %s", szMenuItem[LOWORD(wParam) - 1000]);
MessageBox(hwnd, szText, "MenuApp", MB_OK);*/
}
break;
if (HIWORD(wParam) == BN_CLICKED) {
pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "r");
if(pfile2 == NULL){
system("mkdir C:\\reminder\\data");
pfile = fopen("C:reminder\\data\\backup1.xls", "a");
}
pfile = fopen("database1234.xls", "r");
if(pfile == NULL){
MessageBox(hwnd,"the database for the program was deleted or moved, we will now use the backup,\neverything you enter now will be put into the original file\n and the backup\n the program will try to copy the backup.\n(the program will take care of everything)","database not found",MB_OK);
pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "r");
if(pfile2 == NULL){
system("mkdir C:\\reminder\\data");
pfile = fopen("C:reminder\\data\\backup1.xls", "a");}
}
else{
fclose(pfile);
pfile = fopen("database1234.xls", "a");
}
fclose(pfile);
fclose(pfile2);
pfile = fopen("database1234.xls", "a");
pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "a");
SetWindowText(hwndStatic5,("Getting text..."));
len = GetWindowTextLength(hwndEdit) + 1;
GetWindowText(hwndEdit, text, len);
GetWindowText(hwndEdit2, text2, len);
GetWindowText(hwndEdit3, text3, len);
GetWindowText(hwndEdit4, text4, len);
//SetWindowText(hwnd, text);
SetWindowText(hwndStatic5,("writing to file..."));
fprintf(pfile,"%s,%s,%s,\"%s\"\n",text2,text3,text4,text);
fprintf(pfile2,"%s,%s,%s,\"%s\"\n",text2,text3,text4,text);
//MessageBox(hwnd,text,"text",MB_OK);
fclose(pfile);
fclose(pfile2);
SetWindowText(hwndStatic5,("done"));
SetWindowText(hwndEdit,(""));
SetWindowText(hwndEdit2,(""));
SetWindowText(hwndEdit3,(""));
SetWindowText(hwndEdit4,(""));
}
break;
case WM_CLOSE :
if(MessageBox(hwnd,"are you sure you want to exit","Exit",MB_YESNO) == IDYES){
DestroyWindow(hwnd);}
else{
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Edit Control" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("Edit control"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
1000, 420, 280, 350, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
-
One among many problems:
SourceForge.net: Common mistakes and errors - cpwiki
Compile warnings:
Warning 1 warning C4130: '!=' : logical operation on address of string constant 132
Warning 2 warning C4130: '==' : logical operation on address of string constant 138
Warning 3 warning C4101: 'hSubMenu2' : unreferenced local variable 37
Warning 4 warning C4101: 'szText' : unreferenced local variable 38
Warning 5 warning C4100: 'nCmdShow' : unreferenced formal parameter 317
Warning 6 warning C4100: 'lpCmdLine' : unreferenced formal parameter 317
Warning 7 warning C4100: 'hPrevInstance' : unreferenced formal parameter 316
Warning 8 warning C6001: Using uninitialized memory 'buff1': Lines: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 42, 102, 104, 106, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 119, 122, 123, 124, 127 127
Warning 9 warning C6001: Using uninitialized memory 'character': Lines: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 42, 102, 104, 106, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 119, 122, 123, 124, 127, 130 130
Warning 10 warning C4702: unreachable code 234
Warning 11 warning C4702: unreachable code 236
Warning 12 warning C4702: unreachable code 239
Warning 13 warning C4702: unreachable code 242
Warning 14 warning C4702: unreachable code 247
Warning 15 warning C4702: unreachable code 254
Warning 16 warning C4702: unreachable code 261
Warning 17 warning C4702: unreachable code 264
Warning 18 warning C4700: uninitialized local variable 'buff1' used 127
Warning 19 warning C4700: uninitialized local variable 'character' used 130
Pay heed to using uninitialized memory and uninitialized local variables. The former will cause a crash; the later may cause a crash.
You have comparison bugs, too.
Oh yes, and learn to indent properly.