Skip to main content
gamedev.net
🔒 Locked

MessageBox acting screwy.

Started by Gwahir Dec 21, 2002 at 10:23 PM 9 replies 600+ views
Original Post
Gwahir
Gwahir
Ok, I am making a simple program to load a text file, parse it for certain info, and then do various irrelevant things. I am using the Common Dialog Box function GetOpenFileName for obtaining the file name, then using MessageBox to notify the user of succes or failure of the file load. But MessageBox is displaying the wrong thing, for no good reason. Now, I am explicitly telling the messagebox what to say, like this:
if(f_ptr == NULL)
   	MessageBox(AppWindow, "Invalid File", NULL, MB_OK);
   else
   	MessageBox(AppWindow, "File Loaded Sucessfully", NULL MB_OK);
 
What is freaky is that when the program is run, the messagebox displayed does not contain "Invalid File" or "File Loaded Sucessfully". It contains the filename I selected in GetOpenFileName earlier in the program, except it chops it off at the first space. Thus if I load ''C:/My Documents/test.txt'' it displays ''Documents/test.txt'' in the messagebox. Other cases: If I click cancel and don''t load a file in the GetOpenFileName dialog box, I get a valid messagebox, saying "Invalid File". If the filename contains no spaces, I get an "Invalid File" message, even if I am as sure as possible that the file is valid. But I can''t debug that part till I know that my messageboxes are displaying what they should and what they should according to the program. I am guessing there is somthing about memory that is screwing up, but for the life of me I don''t know.
Zipster
Zipster
What is f_ptr?
Gwahir
Gwahir
Well, I do it in two separate functions, called one right after the other. So the OPENFILESTRUCT goes out of scope before I ever open a messagebox. But here''s my code:


  

void PickFile(void)
{
OPENFILENAME FileInfo;

FileInfo.lStructSize = sizeof(OPENFILENAME);
FileInfo.hwndOwner = NULL;
FileInfo.lpstrFilter = "Edit Files(*.txt)\0" "*.txt\0\0";
FileInfo.lpstrCustomFilter = NULL;
FileInfo.nMaxCustFilter = NULL;
FileInfo.lpstrFile = "\0";
FileInfo.nMaxFile = 512; // this should be long enough

FileInfo.lpstrFileTitle = NULL;
FileInfo.nMaxFileTitle = NULL;
FileInfo.lpstrInitialDir = "C:/";
FileInfo.lpstrTitle = NULL;
FileInfo.Flags = OFN_FILEMUSTEXIST | OFN_NOREADONLYRETURN | OFN_HIDEREADONLY;
FileInfo.lpstrDefExt = "txt";

GetOpenFileName(&FileInfo);

FileName = FileInfo.lpstrFile;

}


void LoadFile(void)
{
f_ptr = NULL;

f_ptr = fopen("C:/test.txt","r+");

if(f_ptr == NULL)
MessageBox(AppWindow, "Invalid File", NULL, MB_OK);
else
MessageBox(AppWindow, "File Loaded Sucessfully", NULL, MB_OK);

fclose(f_ptr);

}



I call these two functions directly after eachother. FileName is a global string class.

What is really weird is that this is the program that gives those wacky results. Notice that I don''t even use the filename of the file I chose to open, I explicetly specified the file to open. C:/test.txt. I know it exists. Yet the results are the same as those I posted in the title post.
glassJAw
glassJAw
quote:
Original post by Gwahir

if(f_ptr == NULL)
MessageBox(AppWindow, "Invalid File", NULL, MB_OK);
else
MessageBox(AppWindow, "File Loaded Sucessfully", NULL MB_OK );




Forgot a comma after NULL on the last line

[edited by - glassJAw on December 22, 2002 4:15:07 PM]
LordLethis
LordLethis
1. You need to assign a buffer to lpstrFile. You actually just set it to a 1 byte buffer.
2. You need to type '\\' to get \ in a string.


    
OPENFILENAME FileInfo;
FileInfo.lStructSize = sizeof(OPENFILENAME);
FileInfo.hwndOwner = NULL;
FileInfo.lpstrFilter = "Edit Files(*.txt)\0*.txt\0\0";
FileInfo.lpstrCustomFilter = NULL;
FileInfo.nMaxCustFilter = NULL;
FileInfo.lpstrFile = (char*)malloc(512);
FileInfo.nMaxFile = 512; // this should be long enough

FileInfo.lpstrFileTitle = NULL;
FileInfo.nMaxFileTitle = NULL;
FileInfo.lpstrInitialDir = "C:\\";
FileInfo.lpstrTitle = NULL;
FileInfo.Flags = OFN_FILEMUSTEXIST | OFN_NOREADONLYRETURN | OFN_HIDEREADONLY;
FileInfo.lpstrDefExt = "txt";
GetOpenFileName(&FileInfo);
FILE* f_ptr = fopen(FileInfo.lpstrFile,"r+");
if(f_ptr == NULL)
MessageBox(0, "Invalid File", NULL, MB_OK);
else
MessageBox(0, "File Loaded Sucessfully", NULL, MB_OK);
fclose(f_ptr);
free(FileInfo.lpstrFile);
return 0;


[edited by - LordLethis on December 22, 2002 4:45:04 PM]
Gwahir
Gwahir
Ok... I think I see... Now let me make it clear that the file aquisition code works. If I put ''MessageBox(AppWindow, FileName.c_str(), NULL, MB_OK) the messagebox displays the full path of the file I want to load.

So what is happening is that the filename string is written to that address in memory, but the computer does not recognize that that memory space is in use, which leads to problems? I''ll give it a try.

quote:
Forgot a comma after NULL on the last line


Copying error.

quote:
2. You need to type ''\\'' to get \ in a string.


Dosn''t a forward slash, ''/'', work as well as a double backslash? I had some trouble with that when I was first learning this stuff, but I wrote a quck function to switch every ''\'' to a ''/'' and after that it seemed to work fine.







Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.