Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have been trying to create a file within this code and I tried every way. This is a c++ desktop app so I

Hello, I have been trying to create a file within this code and I tried every way. This is a c++ desktop app so I think that the code may be different for this. please help me. Write the CREATE_FILE(string) function so that it would create a file and write the string thats passed in it (ios::app). I used visual studio and make sure that it works.

source code:

#include "stdafx.h" #include "BKC.h" #include"Resource.h" #include #include #include #include #include #include #include #include #include #include

#define MAX_LOADSTRING 100 using namespace std; using std::fstream; // Global Variables: HINSTANCE hInst; // current instance WCHAR szTitle[MAX_LOADSTRING]; // The title bar text WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name OPENFILENAME ofn;

string NAME = "temp"; TCHAR szFile[208]; TCHAR buffer[208]; // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); void CREATE_FILE(string);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_BKC, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance);

// Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; }

HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BKC));

MSG message; // Main message loop: while (GetMessage(&message, nullptr, 0, 0)) { if (!TranslateAccelerator(message.hwnd, hAccelTable, &message)) { TranslateMessage(&message); DispatchMessage(&message); } }

return (int) message.wParam; }

// // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BKC)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = GetSysColorBrush(COLOR_APPWORKSPACE); wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_BKC); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassExW(&wcex); }

// // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

if (!hWnd) { return FALSE; }

ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);

return TRUE; }

// // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hwndEdit; HWND hwndButton; switch (message) { case WM_CREATE: { hwndEdit = CreateWindowW(L"edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 50, 50, 150, 20, hWnd, (HMENU)1, NULL, NULL); hwndButton = CreateWindowW(L"button", L"Set file name", WS_VISIBLE | WS_CHILD, 50, 100, 180, 50, hWnd, (HMENU)2, NULL, NULL); CreateWindow(TEXT("button"), TEXT("Select drivers"), WS_VISIBLE | WS_CHILD, 30, 250, 200, 175, hWnd, (HMENU)3, NULL, NULL); CreateWindow(TEXT("button"), TEXT("Quit"), WS_VISIBLE | WS_CHILD, 230, 250, 200, 175, hWnd, (HMENU)4, NULL, NULL);

break;

} case WM_COMMAND: { if (LOWORD(wParam) == 2) {

int len = GetWindowTextLengthW(hwndEdit) + 1; TCHAR text[100]; GetWindowTextW(hwndEdit, text, len); MessageBox(NULL, text, _T("File Name"), MB_OK); wstring buf(&text[0]); string temp(buf.begin(), buf.end()); NAME = temp; } else if (LOWORD(wParam) == 3) { ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFile = szFile; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0"); ofn.nFilterIndex = 1; ofn.lpstrFileTitle = buffer; ofn.nMaxFileTitle = 208; ofn.lpstrInitialDir = _T("C:\\"); ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; GetOpenFileName(&ofn); string FILE_PATH, FILE_NAME; #ifndef UNICODE FILE_NAME = buffer; FILE_PATH = szFile; #else wstring WFILE_NAME = buffer; wstring WFILE_PATH = szFile; FILE_NAME = string(WFILE_NAME.begin(), WFILE_NAME.end()); FILE_PATH = string(WFILE_PATH.begin(), WFILE_PATH.end()); #endif // !UNICODE string TRUE_PATH = FILE_PATH.substr(0, FILE_PATH.find(FILE_NAME)); CREATE_FILE(TRUE_PATH); } else if (LOWORD(wParam) == 4) { PostQuitMessage(0); } else if (LOWORD(wParam) == IDM_ABOUT) { DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); } else if (LOWORD(wParam) == IDM_EXIT) { DestroyWindow(hWnd); }

} break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code that uses hdc here... EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE;

case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }

void CREATE_FILE(string PATH) {

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions