CString newline

This is a discussion on CString newline within the Windows Programming forums, part of the Platform Specific Boards category; How would you create a new line in a CString? Like, lets say I have: "asdfasdfashdfsdl;j;lkjasfl;kjasdf", and I wanted it ...

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    CString newline

    How would you create a new line in a CString? Like, lets say I have: "asdfasdfashdfsdl;j;lkjasfl;kjasdf", and I wanted it to be put on two lines, not one. So that when I open up the file, it says on line 1: "asdfasdfashdfsdl;", and on line 2: "j;lkjasfl;kjasdf". Also, how would I read each line?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Just insert a newline where ever you want.

    gg

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    I'm taking a new direction on this at the time. What I'm doing now is storing the title, date, author, and poem (all max 255 characters) into arrays (seperate ones). Then, I use body (which is CString) to format how the data will be saved (title::author::date\n\npoem). I can get all the information from the edit boxes fine, and I can save it the way I want. My new problem arises when trying to open it.

    I was reading a post in the C Programming boards here, and Prelude (I believe) showed how to read in information. But, whenever I try to open the file, I get crap as text. As far as I know it's loading it all in correctly, just not displaying it correctly. Here's what I'm using for it:

    Code:
    //* in header file:  DWORD dwBytesWritten *//
    
    // start code
    char title[255], date[255], author[255], poem[255];
    char *body = NULL;
    
    ZeroMemory(&ofn, sizeof(ofn));
    
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner   = NULL;
    ofn.lpstrFilter = "\0Poetry Editor File (*.pef)\0*.pef\0\0";
    
    if(GetOpenFileName(&ofn) == TRUE){
    	hFile = CreateFile((LPCTSTR)ofn.lpstrFile,
                                               GENERIC_READ,
    		            0,
    		            NULL,
    		            OPEN_EXISTING,
    		            FILE_ATTRIBUTE_NORMAL,
    		            NULL);
    
    if(hFile != INVALID_HANDLE_VALUE){
    
        if(sscanf((LPCTSTR)body, "%255[^::]%*c%255[^::]%*c%255[^::]%*c%1024[^::]", title, author, date, poem)){
    
               if(ReadFile(hFile, body, strlen(body), &dwBytesWritten, NULL) == FALSE){
                   AfxMessageBox("Unable to read poem from file!");
               }
         }
    
         if(CloseHandle(hFile) == FALSE){
                  AfxMessageBox("Unable to close HANDLE hFile properly!");
                 }
         }
    
         m_poem.SetWindowText(poem);
         m_authorsname.SetWindowText(author);
         m_poemtitle.SetWindowText(title);
         m_datewritten.SetWindowText(date);
    }
    :<<EDIT>>: This program is a dialog, and I was also wondering something else. How would I load certain text stored in a file at loadtime? I've tried putting the code into the InitDialog() function just before the "return TRUE;" at the bottom of the function. Everytime I do it though, it crashes.
    Last edited by Quantrizi; 04-15-2004 at 02:41 PM.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I use body (which is CString)...I get crap as text<<

    Your code actually declares 'body' as type char* and you don't allocate any memory to which it points.

    Putting your code in the InitDialog() function should be ok - once you fix the above mentioned error.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Ken Fitlike
    >>I use body (which is CString)...I get crap as text<<

    Your code actually declares 'body' as type char* and you don't allocate any memory to which it points.

    Putting your code in the InitDialog() function should be ok - once you fix the above mentioned error.
    Thank you...but I forgot to mention that the code I posted above isn't what I use to get the author's name. Here's the code I use:

    Code:
    BOOL CPoetryEditorDlg::GetAuthorName(){
    	char *an = NULL;
    
    	hFile = CreateFile("authors_name.pef",
    					   GENERIC_READ,
    					   0,
    					   NULL,
    					   OPEN_EXISTING,
    					   FILE_ATTRIBUTE_NORMAL,
    					   NULL);
    
    	if(hFile != INVALID_HANDLE_VALUE){
    		if(ReadFile(hFile, an, strlen(an), &dwBytesWritten, NULL) != TRUE){
    			AfxMessageBox("Unable to read author's name from file!");
    
    			return FALSE;
    		}
    
    		m_authorsname.SetWindowText(an);
    
    		if(CloseHandle(hFile) != TRUE){
    			AfxMessageBox("Unable to close HANDLE hFile correctly!");
    
    			return FALSE;
    		}
    
    		return TRUE;
    	}
    
    	return TRUE;
    }
    There's no compiler errors, just that it keeps crashing when I try to run it if I do:

    Code:
    BOOL CPoetryEditorDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    	
                    /* code that has no use for my problem */
    
    	// TODO: Add extra initialization here
    	if(GetAuthorName() != TRUE){
    		AfxMessageBox("Unable to get author's name!");
    	}
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    The only problem I can actually see that might be it is not calling my GetAuthorName() function after the OnInitDialog(); function at the top.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You have exactly the same problem: you're declaring pointers and not allocating any memory for them. This time it's the CPoetryEditorDlg::GetAuthorName function which declares 'an' as a char* and then tries to use it without allocating any memory for it. This sort of thing will blow up in your face at runtime, everytime (unless, maybe, with msvc6 in debug mode...).

    Use 'malloc' or 'new' to allocate memory (and 'free' or 'delete[]' it when done).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Ken Fitlike
    You have exactly the same problem: you're declaring pointers and not allocating any memory for them. This time it's the CPoetryEditorDlg::GetAuthorName function which declares 'an' as a char* and then tries to use it without allocating any memory for it. This sort of thing will blow up in your face at runtime, everytime (unless, maybe, with msvc6 in debug mode...).

    Use 'malloc' or 'new' to allocate memory (and 'free' or 'delete[]' it when done).
    Thank you. I didn't think about that...dang it. Thanks again. I'll try it and see if it works as planned.

  8. #8
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    It seems to work pretty good. Only problem is that after getting the text, and displaying it, it also shows strange characters. Here's the new code I'm using (non-changed parts are "/*...*/"'ed out):

    Code:
    BOOL CPoetryEditorDlg::GetAuthorName(){
    
    	// storage for getting author's name
    	char *an = new char[255];
    
                   /*...*/
    
    	delete an;
    
    	return TRUE;
    }

  9. #9
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Clear the array prior to use and immediately after you've created it and that should limit the garbage character problems. Use memset or ZeroMemory to set the contents of the array to zero. Also make sure to use 'delete[]' to delete an array:
    Code:
    BOOL CPoetryEditorDlg::GetAuthorName(){
    
    	// storage for getting author's name
    	char *an = new char[255];
            ZeroMemory(an,255);
    
                   /*...*/
    
    	delete[] an;
    
    	return TRUE;
    }
    You may also save some hassle by creating a const variable instead of using '255' everywhere. That way, if you need to change its value you only need to do so in one place rather than sift through the code to find and change all those '255's to some other value.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Writing CObjects with Serialization to CArchive
    By ruben_gerad in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2006, 07:25 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 10:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21