Thread: Converting from CString to char

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Converting from CString to char

    I'm trying to make an HTML tag converter.. it will accept a file name, and convert all HTML tags within it to the specified uppercase or lowercase. However, I need to convert some CStrings to chars to be used as the filename. This is what I have so far, but it's crashing when I run it, and debug points to my conversion.



    Code:
    void CHTMLConvertDlg::OnConvert() 
    {
    	UpdateData(TRUE);
    	m_sFileName.Replace('\\','/');
    	m_sFileNameOut.Replace('\\','/');
    
    	ifstream fin;
    	ofstream fout;
    
    	char *fileNameIn = new char[m_sFileName.GetLength()];
    	char *fileNameOut = new char[m_sFileNameOut.GetLength()];
    
    	for (int i = 0; i < m_sFileName.GetLength(); i++)
    	{
    		fileNameIn[i] = m_sFileName.GetAt(i);
    	}
    
    	for (i = 0; i < m_sFileNameOut.GetLength(); i++)
    	{
    		fileNameOut[i] = m_sFileName.GetAt(i);
    	}
    
    	fin.open(fileNameIn, ios::nocreate);
    	fout.open(fileNameOut);
    
    	delete[] fileNameIn;
    	delete[] fileNameOut;
    
    	if (!fin.fail())
    	{
    		while (!fin.eof())
    		{
    			BeginWaitCursor();
    			char line[255];
    			fin.getline(line, 255, '\n');
    			//fin.close();
    			CString string = line;
    
    			int firstOffset = 0;
    			int secondOffset = 0;
    			if (!string.IsEmpty())
    			{
    				while (firstOffset != -1)
    				{
    					int firstOffset = string.Find('<', firstOffset);
    					int secondOffset = string.Find('>', firstOffset);
    					CString smallString;
    					for (int i = 0; i < (secondOffset - firstOffset); i++)
    					{
    						smallString =+ string.GetAt(firstOffset + 1);
    					}
    
    					if (m_bCase)
    					{
    						smallString.MakeUpper();
    					} else {
    						smallString.MakeLower();
    					}
    
    					for (i = 0; i < (secondOffset - firstOffset); i++)
    					{
    						char temp;
    						temp = smallString.GetAt(i);
    
    						string.SetAt(firstOffset + 1 + i, temp);
    					}
    					
    				} 
    				
    				if (firstOffset == -1)
    				{
    					char *tempString = new char[string.GetLength()];
    					
    					for (i = 0; i < string.GetLength(); i++)
    					{
    						tempString[i] = string.GetAt(i);
    					}
    
    
    					fout.write(tempString, string.GetLength());
    					delete[] tempString;
    				}
    
    			}
    		} 
    		if (fin.eof())
    		{
    			EndWaitCursor();
    			AfxMessageBox("Finished");
    		}
    
    
    
    
    
    	}
    
    
    
    
    }
    How can I convert CString to char?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Do you really need the conversion to char* ? CString allows implicit conversion to a const char* (LPCSTR), so just pass the CString to fstream::open().

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    The previous reply is correct but also you must add an additional char when newing a char array

    change this
    Code:
    
    
    char *fileNameIn = new char[m_sFileName.GetLength()];
    	char *fileNameOut = new char[m_sFileNameOut.GetLength()];
    to this
    Code:
    char *fileNameIn = new char[m_sFileName.GetLength()+1];
    	char *fileNameOut = new char[m_sFileNameOut.GetLength()+1];
    zMan

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Mmm right you are. That was surely the cause of my memory leaks.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    I did not see this before

    I didn't see this before but....

    Code:
    	fin.open(fileNameIn, ios::nocreate);
    	fout.open(fileNameOut);
    
    can be changed to 
    	fin.open(m_sFileName.GetBuffer(m_sFileName.GetLegth()+1), ios::nocreate);
    	fout.open(m_sFileName.GetBuffer(m_sFileName.GetLegth()+1),
    zMan

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Oh come on! Do you really believe that 15 months later he would still be looking for input here?!?!?!?!?

    Bumping old threads is against the rules, bumping ancient threads is simply daft!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM