![]() |
|
#1
|
|||
|
|||
[winapi] edit control is lying to me...code says a thousand words:
CPP / C++ / C Code:
EDIT: forgot to add, GetLastError tells me that nothing went wrong (zero error code and empty string returned by FormatMessage). also, dbgmsg and errmsg are defined as: CPP / C++ / C Code:
Last edited by ubergeek : 13-May-2005 at 20:27.
Reason: fixed formatting issue
|
|
#2
|
|||
|
|||
|
I'm afraid I can't really find what's wrong in your code, unless you should use GlobalAlloc() instead of new, but here's my saving function if that helps at all:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
|
Quote:
Here's the sequence: You declare filetext to be a pointer to char and set its value to point to a dynamically allocated block of chars. Then you set the value of filetext to point to the address of the string literal "" (some place in program space that's not available to you for writing). So, first problem: You can never delete the dynamically allocated memory since you have lost its address (that's known as a memory leak). Then, you call a function (it doesn't really matter what function) and pass the value of the pointer to the function. I don't care what the function tries to do with the pointer, the fact is that the value of the pointer in the calling function is not changed. (If the function does try to write something to that address, the behavior is undefined, since addresses of string literals are not supposed to be used for writing. The progam might crash --- or not.) In any case, when your comparison statement tests to see if the value if filetext is still equal to the same value that it was before the function call: it is. The fix for this particular problem: In general you could change this CPP / C++ / C Code:
to the following: CPP / C++ / C Code:
or maybe to this: CPP / C++ / C Code:
Then, after the function call, change this CPP / C++ / C Code:
to something else. Maybe this: CPP / C++ / C Code:
or this CPP / C++ / C Code:
or something similar. Don't forget to delete [] filetext sometime before your program terminates. Note that for this particular function, GetWindowText(), you don't have to initiliaze the string to be empty before the call; just allocate memory, call the function and test the length of the string after the function returns. In other words, the strcpy() that I show above is not really necessary. (It doesn't do any particular harm, but it clutters up the code with unnecessary activity.) Also, the function returns the length of the string, so you only need to test its return value. That is, instead of testing the return string to see if it's empty, you could do something like this: CPP / C++ / C Code:
Regards, Dave |
|
#4
|
|||
|
|||
|
thanks Dave for catching that error. I thought that a dynamically allocated array could be treated like a regular char arry, I guess I was wrong. Here is the working saveFile function and its two helper functions, in case anyone is interested:
CPP / C++ / C Code:
the replacing of \ with \\ appears to be necessary; otherwise the files refuse to open (winxp) |
|
#5
|
|||
|
|||
|
Quote:
|
|
#6
|
|||
|
|||
|
interestingly enough, I'm pretty sure I tried that too. Maybe I just didn't implement the code properly, or it could have been a compiler peculiarity (Dev-C++ 4.9.9.2). Either way, I might try it again--It would save a lot of memory because / is the same number of characters as \, so I wouldn't have to allocate a whole new string.
|
|
#7
|
|||
|
|||
|
What's with the embedded nulls in the string?
CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
|
that's Microsoft for you. Those are the items that will appear in the Save as Type drop-down in the SaveAs... dialog box. It will appear as "Text Files" "Geekedit Files" "All Files" (in a drop-down). GetSaveFileName() parses that string like this:
For first item, scan until first \0. From there until the next \0 is the filter to associate with that item. (as in: text files are *.txt). Etc. To avoid buffer overruns, the function needs to know where the string ends, hence the \0 at the end. Normally when you type something like CPP / C++ / C Code:
MSDN on GetSaveFileName: http://msdn.microsoft.com/library/de...vefilename.asp and the OPENFILENAME structure, where lpstrFilter is discussed: http://msdn.microsoft.com/library/de...enfilename.asp Quote:
|
|
#9
|
|||
|
|||
|
Quote:
|
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to check a string value read from an edit box | E2STE | MS Visual C++ / MFC Forum | 1 | 06-May-2005 01:19 |
| Help! Some basal questions about MFC | xutingnjupt | MS Visual C++ / MFC Forum | 1 | 05-Dec-2004 03:38 |
| Detecting a mouse click outside of control | arran.s | MS Visual C++ / MFC Forum | 1 | 27-Oct-2004 06:01 |
| RichEdit Demo | Max Payne | MS Visual C++ / MFC Forum | 3 | 06-Oct-2004 07:19 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The