проблема с выводом файла
Добавлено: 14 сен 2005, 23:14
полный код программы:
#include <windows.h>
#include <iostream>
#include <fstream>
#include <io.h>
#include <fcntl.h> /* Needed only for _O_RDWR definition */
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define ID_EDIT 101
#define ID_BSAVE 102
#define ID_BOPEN 103
HINSTANCE hInst;
LRESULT CALLBACK TextEditorWndProc(HWND, UINT, UINT, LONG);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndClass;
MSG Msg;
char szClassName[] = "TextEditor";
hInst = hInstance;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = TextEditorWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = szClassName;
if(!RegisterClass(&WndClass))
{
MessageBox(NULL,"Cannot register class", "Error", MB_OK);
return 0;
}
hWnd = CreateWindow(szClassName, "Lab1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
317, 300,
NULL, NULL,
hInstance, NULL);
if(!hWnd)
{
MessageBox(NULL,"Cannot create window", "Error", MB_OK);
return 0;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK TextEditorWndProc(HWND hWnd, UINT Message,
UINT wParam, LONG lParam)
{
static HWND hEditWnd;
static HWND hBSaveWnd;
static HWND hBOpenWnd;
RECT Rect;
switch(Message){
case WM_CREATE:
GetClientRect(hWnd,&Rect);
hEditWnd = CreateWindow("EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
5,5,300,200,
hWnd,
(HMENU)ID_EDIT,// edit control ID
hInst,
NULL);
hBSaveWnd = CreateWindow("BUTTON", "Save as..",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT,
10,210,145,52,
hWnd,
(HMENU)ID_BSAVE,// edit control ID
hInst,
NULL);
hBOpenWnd = CreateWindow("BUTTON", "Open file..",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT,
155,210,145,52,
hWnd,
(HMENU)ID_BOPEN,// edit control ID
hInst,
NULL);
return 0;
case WM_SETFOCUS:
SetFocus(hEditWnd);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam)){
//-----------------------------------------------------------------
case ID_BSAVE:
break;
case ID_BOPEN:
OPENFILENAME OF;
char szFile[260];
HANDLE hf;
ZeroMemory(&OF, sizeof(OF));
OF.lStructSize = sizeof(OF);
OF.hwndOwner = hWnd;
OF.lpstrFile = szFile;
# OF.lpstrFile = '\0';
OF.nMaxFile = sizeof(szFile);
OF.lpstrFilter = "All\0*.*\0";
OF.nFilterIndex = 1;
OF.lpstrFileTitle = NULL;
OF.nMaxFileTitle = 0;
OF.lpstrInitialDir = NULL;
OF.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&OF)==TRUE)
hf = CreateFile(OF.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
//Open file for outputing on screen
/*ifstream fin(szFile,ios_base::in);
for(int i=0; i<1024; i++)
fin>>file_out;
fin.close();*/
int fh;
unsigned int nbytes = 1024, bytesread;
char file_out[1024];
fh = _open(szFile, _O_RDONLY);
bytesread = _read(fh, file_out, nbytes );
_close( fh );
SendMessage(hEditWnd, WM_SETTEXT,0,(LPARAM)file_out);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,Message,wParam,lParam);
}
когда открываешь какой-то текстовый фаил, в буфер выводит приблизительно такое: ММММММММММММММММММММММММММММММММММММММММММ
ММММММММММММММММММ
функции вывода:
int fh;
unsigned int nbytes = 1024, bytesread;
char file_out[1024];
fh = _open(szFile, _O_RDONLY);
bytesread = _read(fh, file_out, nbytes );
_close( fh );
пробовал также
ifstream fin(szFile,ios_base::in);
for(int i=0; i<1024; i++)
fin>>file_out;
fin.close();
результат один и тот же
помогите пожалуйста
#include <windows.h>
#include <iostream>
#include <fstream>
#include <io.h>
#include <fcntl.h> /* Needed only for _O_RDWR definition */
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define ID_EDIT 101
#define ID_BSAVE 102
#define ID_BOPEN 103
HINSTANCE hInst;
LRESULT CALLBACK TextEditorWndProc(HWND, UINT, UINT, LONG);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndClass;
MSG Msg;
char szClassName[] = "TextEditor";
hInst = hInstance;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = TextEditorWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = szClassName;
if(!RegisterClass(&WndClass))
{
MessageBox(NULL,"Cannot register class", "Error", MB_OK);
return 0;
}
hWnd = CreateWindow(szClassName, "Lab1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
317, 300,
NULL, NULL,
hInstance, NULL);
if(!hWnd)
{
MessageBox(NULL,"Cannot create window", "Error", MB_OK);
return 0;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK TextEditorWndProc(HWND hWnd, UINT Message,
UINT wParam, LONG lParam)
{
static HWND hEditWnd;
static HWND hBSaveWnd;
static HWND hBOpenWnd;
RECT Rect;
switch(Message){
case WM_CREATE:
GetClientRect(hWnd,&Rect);
hEditWnd = CreateWindow("EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
5,5,300,200,
hWnd,
(HMENU)ID_EDIT,// edit control ID
hInst,
NULL);
hBSaveWnd = CreateWindow("BUTTON", "Save as..",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT,
10,210,145,52,
hWnd,
(HMENU)ID_BSAVE,// edit control ID
hInst,
NULL);
hBOpenWnd = CreateWindow("BUTTON", "Open file..",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT,
155,210,145,52,
hWnd,
(HMENU)ID_BOPEN,// edit control ID
hInst,
NULL);
return 0;
case WM_SETFOCUS:
SetFocus(hEditWnd);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam)){
//-----------------------------------------------------------------
case ID_BSAVE:
break;
case ID_BOPEN:
OPENFILENAME OF;
char szFile[260];
HANDLE hf;
ZeroMemory(&OF, sizeof(OF));
OF.lStructSize = sizeof(OF);
OF.hwndOwner = hWnd;
OF.lpstrFile = szFile;
# OF.lpstrFile = '\0';
OF.nMaxFile = sizeof(szFile);
OF.lpstrFilter = "All\0*.*\0";
OF.nFilterIndex = 1;
OF.lpstrFileTitle = NULL;
OF.nMaxFileTitle = 0;
OF.lpstrInitialDir = NULL;
OF.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&OF)==TRUE)
hf = CreateFile(OF.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
//Open file for outputing on screen
/*ifstream fin(szFile,ios_base::in);
for(int i=0; i<1024; i++)
fin>>file_out;
fin.close();*/
int fh;
unsigned int nbytes = 1024, bytesread;
char file_out[1024];
fh = _open(szFile, _O_RDONLY);
bytesread = _read(fh, file_out, nbytes );
_close( fh );
SendMessage(hEditWnd, WM_SETTEXT,0,(LPARAM)file_out);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,Message,wParam,lParam);
}
когда открываешь какой-то текстовый фаил, в буфер выводит приблизительно такое: ММММММММММММММММММММММММММММММММММММММММММ
ММММММММММММММММММ
функции вывода:
int fh;
unsigned int nbytes = 1024, bytesread;
char file_out[1024];
fh = _open(szFile, _O_RDONLY);
bytesread = _read(fh, file_out, nbytes );
_close( fh );
пробовал также
ifstream fin(szFile,ios_base::in);
for(int i=0; i<1024; i++)
fin>>file_out;
fin.close();
результат один и тот же
помогите пожалуйста