Login(Email) Password Forget Password? Account Settings
Home ASP.net System Info C# Books Java Script Visual C++(MFC) C/C++ Win API Java Contact Us
How to read a text file line by line?
void CClassName::ReadTextFile(LPCTSTR pszFileName, CStringArray& arrLines) 
{ 
   arrLines.RemoveAll(); 
   CString strLine; 
   TRY 
   { 
      CStdioFile file(pszFileName, CFile::modeRead); 
      while(file.ReadString(strLine)) 
         arrLines.Add(strLine); 
   } 
   CATCH_ALL(e) 
   { 
      e->ReportError(); // shows what's going wrong 
   } 
   END_CATCH_ALL 
}
Above code segment will read file line by line and add the lines into arrLines CstringArray type of variable .Later on you can get these lines from addLines Array.
 How to get the application name?

In case you have MFC Application you can use .

AfxGetApp()->m_pszExeName;
In all other cases the function 'GetModuleFileName()' can be used..

Using STL string

#include <string>
char szAppPath[MAX_PATH] = "";
std::string strAppName;
::GetModuleFileName(0, szAppPath, MAX_PATH);
// Extract name
strAppName = szAppPath;
strAppName = strAppName.substr(strAppName.rfind("\\") + 1);
CString str =strAppName.c_str();
MessageBox(str);

Using CString (MFC)

char szAppPath[MAX_PATH] = " ";
CString strAppName;
::GetModuleFileName(0, szAppPath, MAX_PATH);
// Extract name
strAppName = szAppPath;
strAppName = strAppName.Right(strAppName.ReverseFind('\\') + 1);

                                    

MessageBox(strAppName);

How to convert string to CString (LPSTR)

string str ("Hello");
CString str1=str.c_str();
How to convert from CString to std::string?
CString strSomeCstring ("Hello");

// Use ANSI variant CStringA to convert to char*
std::string strStdString (CStringA (strSomeCstring));
What types of strings are there?
  • 'char*' -> this is also called a C-style string, or an ANSI string.
  • 'wchar_t*' -> this is wide character string, i.e. an 'unsigned short*'
  • 'CString' -> this is a string wrapper class, which is part of the Microsoft Foundation Classes (MFC).
  • 'std::string' -> this is a Standard C++ Class wrapping a char string. It is part of the Standard Template Library, or STL.
  • 'std::wstring' -> this is a Standard C++ Class wrapping a wchar_t string. It is part of the Standard Template Library, or STL.
  • 'BSTR' -> this known as basic string or binary string, and is a pointer to a wide character string used by Automation data manipulation functions.
C++ String: How to convert a numeric type to a string?

First Method

char c[10];
int i = 1234;
sprintf(c, "%d", i);

Second Method

int i = 1234;
CString cs;
cs.Format("%d", i);
How to convert between a 'CString' and a 'BSTR'?
CString to BSTR
CString str("Pakistan");
BSTR bstr =cs.AllocSysString();
::SysFreeString(bstr);
How to convert String to Hexadecimal

sscanf(string, %04X, &your_word16);

How to convert Hexdecimal to String

CString strvalue;
unsigned char Buff[1];
Buff[0] = 0x01;
strvalue.Format("0x0%x",Buff[0])
Pages 1 2