Advertisement

Win32 question (appending a file)...

Started by June 15, 2002 07:44 PM
1 comment, last by Ax 22 years, 3 months ago
Alright, I''m writing a dialog-based Win32 program, which tries writes a file based on the EDITTEXT resource input. I''ve got it writing to a file (or making one if there isn''t), but the problem is I can''t figure out how to make it write to the end, not start over at the beginning when the program is run again. Anyway, any help will be appriciated. Code for the message handler of the dialog box: static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { static HANDLE hFile; switch (msg) { case WM_INITDIALOG: InitializeApp(hwndDlg,wParam,lParam); hFile = CreateFile("FILE.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE|FILE_FLAG_WRITE_THROUGH,NULL); char mobname[255] = " "; char moblevel[4] = " "; char mobcomment[255] = " "; DWORD dwWritten; BOOL bTran; return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: GetDlgItemText(hwndDlg,MOB_NAME,mobname,255); GetDlgItemText(hwndDlg,MOB_LEVEL,moblevel,4); GetDlgItemText(hwndDlg,MOB_COMMENT,mobcomment,255); WriteFile(hFile,mobname,strlen( mobname ) + 1,&dwWritten,NULL); WriteFile(hFile,moblevel,strlen( moblevel ) + 1,&dwWritten,NULL); WriteFile(hFile,mobcomment,strlen( mobcomment ) + 1,&dwWritten,NULL); WriteFile(hFile,"\r\n",2,&dwWritten,NULL); return 1; case IDCANCEL: { CloseHandle("FILE.txt"); EndDialog(hwndDlg,1); return 1; } } break; case WM_CLOSE: EndDialog(hwndDlg,0); return TRUE; } return FALSE; } Thanks, --Ax
-Ax
Add this after CreateFile:

SetFilePointer(hFile, 0, 0, FILE_END);

Also remove FILE_FLAG_WRITE_THROUGH since it slows down file operations and add CloseHandle(hFile) since you're never closing your file, which is a very Bad Thing.

Edit: you should also check if CreateFile succeeded by comparing hFile with INVALID_HANDLE_VALUE. You don't want to write to a file that isn't opened.

[edited by - IndirectX on June 15, 2002 8:51:33 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
Ah, works perfectly.

Thanks,
--Ax
-Ax

This topic is closed to new replies.

Advertisement