I don't know what line you marked.
However, PLEASE try and use std::string and not immediately change over to
C arrays. C character arrays are perfectly fine to use WHEN YOU KNOW HOW
TO USE THEM.
Now you read a line of text from the edit control into a std::string
object.
Then you immediately ask for a pointer to a zero-terminated string using
the method c_str() and type cast into a writeable pointer:
Finally, you try to concatenate more chanracters to the c string...
Exactly HOW do you think string:c_str() should now about your intentions
to concatenate more characters and reserve room for these extra
characters? You should do ALL manipulation on the std::string.
You typecast c_str() to (char*) just to be able to play with the c string.
However, the c string is NOT meant to be played with. It is a read-only
string!
/Per W
On Mon, 11 Jul 2005, Noorez Kassam wrote:
> The open file function of my program appears to be crashing at the line I
> marked, when I run the debugger. There might be some other problem causing
> it. The function is below:
>
> #include "SML.h"
>
> // Function called from WindowProcedure to open text
> void OpenText(HWND main, HWND hEdit)
> {
> ifstream openTextFile; // Handle to file
>
> // Filter files visible in the open dialog box
> char* filter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
>
> std::string inLine;
> char* toEditControl;
>
> int nLength;
>
> // Init the properties of the open dialog box
> getOFNproperties(main, &dialogProperties, currentFileName, filter);
>
> // Show the dialog and get the information
> if( !GetOpenFileName(&dialogProperties) )
> {
> return; // Return if user cancels
> }
>
> openTextFile.open(currentFileName);
> SetDlgItemText(main, IDC_MAIN_EDIT, ""); // Clear the edit contro
>
> int cntr = 1;
>
> // Loop unitl the end of file is reached
> while( !openTextFile.eof() && !openTextFile.fail() )
> {
> nLength = GetWindowTextLength(GetDlgItem(main, IDC_MAIN_EDIT));
> getline(openTextFile,inLine);
> toEditControl = (char*)inLine.c_str();
>
> //A new line must have a "\r\n" so test for both
> if( toEditControl[ strlen(toEditControl) -1 ] != '\r' )
> {
> strcat(toEditControl,"\r");
> }
> if( inLine[ strlen(toEditControl) -1] != '\n' )
> {
> strcat(toEditControl,"\n");
> }
>
> // And the message onto the edit control
> SendMessage( GetDlgItem(main, IDC_MAIN_EDIT), EM_SETSEL,
> (WPARAM)nLength,
> (LPARAM)nLength );
>
> SendMessage( GetDlgItem(main, IDC_MAIN_EDIT), EM_REPLACESEL,
> (WPARAM)FALSE,
> (LPARAM)toEditControl );
> }
>
> untitled = false;
> edited = false;
> openTextFile.close();
> return;
> }
|