Im trying to make a simple script whereby a user browses for a file, then this location can then be used in a variable. Can anyone tell me how this code can be used, and if it is C and not C# or C++.
Ive found several examples on the internet, but if I try to compile these examples I get lots of error codes saying it isnt correctly coded.
EDIT: For reference this is the code that I found that didnt compile:
#include <windows.h>
#include <fstream>
#include <stdafx.h>
int main()
{
printf("declaring...\n");
// Declare
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle
printf("initialising...\n");
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
printf("setting...\n");
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
printf("displaying...\n");
// Display the Open dialog box.
if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
printf("\n");
printf("opened. exiting...\n");
printf("...");
return 0;
}#include <windows.h>
#include <fstream>
int main()
{
printf("declaring...\n");
// Declare
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle
printf("initialising...\n");
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that <strong class="highlight">GetOpenFileName</strong> does not
// use the contents of szFile to initialize itself.
//
printf("setting...\n");
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
printf("displaying...\n");
// Display the Open dialog box.
if (<strong class="highlight">GetOpenFileName</strong>(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
printf("\n");
printf("opened. exiting...\n");
printf("...");
return 0;
}


Sign In
Create Account


Back to top










