Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading a file into a dialog box for editing?

Status
Not open for further replies.

computerwhiz

Programmer
May 1, 2002
28
US
I am having a problem loading a file into an editing dialog box, if the file is too large to load into notepad it won't load into my program. is there a different type of window or dialog box that can hold more text of larger files? I will need to have enough room to load a file with 65,532 bytes and more.

thanks in advance for any help or point in the right direction.

Code:
#include <windows.h>
#include &quot;Main.h&quot;

static char g_szClassName[] = &quot;MyWindowClass&quot;;
static HINSTANCE g_hInst = NULL;
static BOOL ReadyToParse = FALSE;
static LPSTR pszText;
static LPSTR Quote_A=&quot;\&quot;&quot;;
static LPSTR Quote_B=&quot;\&quot;,\&quot;&quot;;
static LPSTR Quote_C=&quot;\&quot;,&quot;;
static LPSTR Quote_D=&quot;,&quot;;
static LPSTR Return=&quot;\r\n&quot;;
static LPSTR Header=&quot;\&quot;Number\&quot;,\&quot;Date\&quot;,\&quot;Remark\&quot;,\&quot;Gross\&quot;,\&quot;Deductions\&quot;,\&quot;Amount Paid\&quot;&quot;;
static int txtpos=0, count=0;
static BOOL Neg=FALSE;

#define IDC_MAIN_TEXT   1001

void Insert(int Pos, LPSTR String)//insert a string into this position
{
}

void Delete(int Pos, int Count)//delete from this position this count of bytes
{
}

void ProcessHeader(int ChkType)//remove unnecassary data from header.
{
}

void ProcessBody(int BdyType, int NumbPgs)//parse body of remittance
{
}

void ProcessFile(HWND hEdit)
{
}

BOOL LoadFile(HWND hMain, HWND hEdit, LPSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;
	char NewWindowTitle[100] = &quot;Check Parser --&quot;;
	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
	OPEN_EXISTING, 0, 0);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwFileSize;
		dwFileSize = GetFileSize(hFile, NULL);
		if(dwFileSize != 0xFFFFFFFF)//decimal 4,294,967,295 
		{
			LPSTR pszFileText;
			pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
			if(pszFileText != NULL)
			{
				DWORD dwRead;
				if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
				{
					pszFileText[dwFileSize] = 0;
					if(SetWindowText(hEdit, pszFileText))
					{
						memcpy(NewWindowTitle+15, pszFileName, strlen(pszFileName));
						SetWindowText(hMain, NewWindowTitle);
						bSuccess = TRUE;
						ReadyToParse = TRUE;
					}
				}
				GlobalFree(pszFileText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
	CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwTextLength;
		dwTextLength = GetWindowTextLength(hEdit);
		if(dwTextLength > 0)
		{
			LPSTR pszText;
			pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
			if(pszText != NULL)
			{
				if(GetWindowText(hEdit, pszText, dwTextLength + 1))
				{
					DWORD dwWritten;
					if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE;
				}
				GlobalFree(pszText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
	OPENFILENAME ofn;
	char szFileName[MAX_PATH];

	ZeroMemory(&ofn, sizeof(ofn));
	szFileName[0] = 0;

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrDefExt = &quot;htm&quot;;

	if(bSave)
	{
		ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
		OFN_OVERWRITEPROMPT;
		ofn.lpstrFilter = &quot;CSV Files (*.csv)\0*.csv\0\0&quot;;
		if(GetSaveFileName(&ofn))
		{
			if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
			{
				MessageBox(hwnd, &quot;Save file failed.&quot;, &quot;Error&quot;,
				MB_OK | MB_ICONEXCLAMATION);
				return FALSE;
			}
		}
	}
	else
	{
		ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
		ofn.lpstrFilter = &quot;Html Files (*.htm)\0*.htm\0All Files (*.*)\0*.*\0\0&quot;;
		if(GetOpenFileName(&ofn))
		{
			if(!LoadFile(hwnd, GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
			{
				MessageBox(hwnd, &quot;Load of file failed.&quot;, &quot;Error&quot;,
				MB_OK | MB_ICONEXCLAMATION);
				return FALSE;
			}
		}
	}
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_CREATE:
		CreateWindow(&quot;EDIT&quot;, &quot;&quot;,
		WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
		ES_WANTRETURN,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);

		SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
		(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
		break;
	case WM_SIZE:
		if(wParam != SIZE_MINIMIZED)
		MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
		HIWORD(lParam), TRUE);
		break;
	case WM_SETFOCUS:
		SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case CM_FILE_OPEN:
			DoFileOpenSave(hwnd, FALSE);
			break;
		case CM_FILE_SAVEAS:
			DoFileOpenSave(hwnd, TRUE);
			break;
		case CM_FILE_EXIT:
			PostMessage(hwnd, WM_CLOSE, 0, 0);
			break;
		case CM_PARSE_FILE:
			ProcessFile(hwnd);
			break;
		case CM_ABOUT:
			MessageBox (NULL, 
			&quot;MBS TEXTBOOK EXCHANGE, INC.\n&quot;
			&quot;Copyright 2003\n\n&quot;
			&quot;Check parser was designed for the easy\n&quot;
			&quot;conversion of MBS Accounts Payable check\n&quot;
			&quot;remittances into .CSV files.  Making it easier to\n&quot;
			&quot;load the data into a spreadsheet.\n\nDesigned by Dave Dartt&quot; ,
			&quot;About...&quot;, 0);
		}
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
   LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX WndClass;
	HWND hwnd;
	MSG Msg;

	g_hInst = hInstance;

	WndClass.cbSize        = sizeof(WNDCLASSEX);
	WndClass.style         = 0;
	WndClass.lpfnWndProc   = WndProc;
	WndClass.cbClsExtra    = 0;
	WndClass.cbWndExtra    = 0;
	WndClass.hInstance     = g_hInst;
	WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	WndClass.lpszMenuName  = &quot;MAINMENU&quot;;
	WndClass.lpszClassName = g_szClassName;
	WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&WndClass))
	{
		MessageBox(0, &quot;Window Registration Failed!&quot;, &quot;Error!&quot;,
			MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
		return 0;
	}

	hwnd = CreateWindowEx(
	WS_EX_CLIENTEDGE,
	g_szClassName, &quot;Check Parser&quot;,
	WS_OVERLAPPEDWINDOW,
	CW_USEDEFAULT, CW_USEDEFAULT, 550, 400,
	NULL, NULL, g_hInst, NULL);

	if(hwnd == NULL)
	{
		MessageBox(0, &quot;Window Creation Failed!&quot;, &quot;Error!&quot;,
			MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
 
Not sure if you have tried the rich edit control.

use CreateWindowEx and define RICHEDIT_CLASS as the class, most of the messages will translate.

Hope this helps
 
is it necessary to load the WHOLE file at startup ?
why not memory-map it and only display portions of it on the screen?
 
all the data of the file is being parsed into pieces in one continuous while loop.
 
anyway you don't need to load the whole file.
You can analyze it by smaller portions.

Ion Filipski
1c.bmp
 
1. There is no problem to load the whole file in memory and then use SetWindowText() to display the text in the window with the provided handle.
2. If you do the above steps in MFC it will work even using an ordinary CEdit control.
3. Use GetLastError() to find out the right error.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top