Can't see why fopen() isn't working

Apr 21, 2010 at 3:28am
For some reason the following code isn't working. fopen always fails but I don't understand why. Is it because of the type of sourceFilename? I had been using TCHAR in the edit field but couldn't then see how to pass this into fopen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char sourceFilename[MAX_PATH] = "";

 ...

hwndSourceFilename = CreateWindow(TEXT("edit"), TEXT("C:\\Temp\\Demo.pdf"), WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 150, 20, hwnd, (HMENU) ID_SOURCEFILENAME, NULL, NULL);

 ...

if (LOWORD(wparam) == ID_BUTTON_ENCRYPT && HIWORD(wparam) == BN_CLICKED)
{
  FILE *sourceFile;
  if ((sourceFile = fopen(sourceFilename,"rb")) == NULL)
  {
    // Always execute here despite file existing on disk.
    break;
  }
  // Do file processing...
  fclose(sourceFile);


Is it a newb coding assumption/error?

Thank you, AJM.
Last edited on Apr 21, 2010 at 3:31am
Apr 21, 2010 at 9:21am
Where is sourceFilename filled in?
Apr 21, 2010 at 11:34am
Well... at what line do you assign a value to sourceFilename? Also, did you give a full or relative path?
Apr 21, 2010 at 9:34pm
You're right, I'm not assigning it. I was confused by another event handler. This raises the next question though:

1. How to I get the text contents of my edit field into fopen()? It's always complaining that the type is wrong.

e.g.

char sourceFilename[MAX_PATH] = "";

Then...

1
2
3
4
len = GetWindowTextLength(hwndSourceFilename) + 1;
GetWindowText(hwndSourceFilename, sourceFilename, len);
FILE *sourceFile;
if ((sourceFile = fopen(sourceFilename, "rb")) == NULL)


Raises this error on the sourceFilename parameter in GetWindowText()...

1
2
error C2664: 'GetWindowTextW' : cannot convert parameter 2 from 'char [260]' to 'LPWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


I've tried all sorts of things but I can't find how to get the text of the control and put that into the sourceFilename variable.

Thank you,
AJM.
Last edited on Apr 21, 2010 at 9:37pm
Apr 21, 2010 at 9:57pm
I've made some progress after following the advise on this page: http://www.velocityreviews.com/forums/t458700-char-to-lpwstr.html


So now the code does this...

1
2
3
4
5
6
7
8
9
10
11
LPWSTR gah = new wchar_t[MAX_PATH];
GetWindowText(hwndSourceFilename, gah, MAX_PATH);
MultiByteToWideChar(CP_ACP, 0, sourceFilename, MAX_PATH, gah, MAX_PATH);
FILE *sourceFile;
if ((sourceFile = fopen(sourceFilename, "rb")) == NULL)
{
	// This is executed ?!?
	SetWindowText(hwnd, gah);
	break;
}
SetWindowText(hwnd, gah);


But, gah is always null, and it ends up evaluating sourceFilename to null too. I don't see why though, the edit field has a valid file path in it:

hwndSourceFilename = CreateWindow(TEXT("edit"), TEXT("C:\\Temp\\Demo.pdf"), WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 150, 20, hwnd, (HMENU) ID_SOURCEFILENAME, NULL, NULL);

Last edited on Apr 21, 2010 at 9:58pm
Apr 21, 2010 at 11:00pm
You don't need to do that. You evidently have _UNICODE defined. Change the project so it's not defined and you'll see ASCII versions of the WIN32 API.
Apr 22, 2010 at 12:42am
@kbw magic, problem solved. Thank you.

She's all good now and what's even better, the GUI port of the code has hung together so now it's just a matter of polishing up the interface.

Thanks to all those that helped, it's much appreciated. I'm learning C++ but a project for work has me creating something beyond my skill set, for which I don't have time to get there on my own. Though this has gone a huge way to increasing my knowledge and understanding of C++.

Cheers,
AJM.
Topic archived. No new replies allowed.