Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a file with some text. I want to do 2 tasks
1.append the file with mac_address at end of the text when i open it.
2.Replace first char in the string with Letter 'P'

C++
while (readFile.ReadString(strLine))
{

CString s=strLine;
.....
if(s[0]=='L')
{
for(int temp=0;temp<18;temp++)
{

 s[m]=mac_address[temp];//here is the error

}
s[0]=_T('P');// here is the error

}
Posted
Updated 5-Apr-12 0:12am
v5
Comments
chaiein 5-Apr-12 6:14am    
I solved appending string to end of the file as in solution3. How to replace character at s[0] to 'p' where s is a CString

CString constructor should do the job, namely:
C++
CString s(mac_address);


Beware if you are doing a UNICODE build then the constructor performs the necessary conversion of the provided chars into wide char ones.

[update]
C++
if ( s.Left(1)==_T("L") )
{
  s = _T("P");
  for(int i = 1; i < 18; i++)
  {
    s += mac_address[i];
  }
}

[/update]
 
Share this answer
 
v3
Comments
chaiein 4-Apr-12 6:47am    
I need s[1] to be mac_address[0] where s is string and mac_address is char I think this s(mac_address) wont copy as required
chaiein 4-Apr-12 6:48am    
what sholud i include in s(mac_address)function.
CPallini 4-Apr-12 6:52am    
Could you please elaborate a bit your needs? Why should you 'encode' the mac_address into a CString?
chaiein 4-Apr-12 7:10am    
Please check my updated question.
chaiein 4-Apr-12 7:10am    
I have updated the question please check.
Use CString::SetAt(), to change the value at any index.
C++
s[m]=mac_address[temp]; 
//can be written as
if(m>=0 && m<s.getlength())>
{
   s[m]=mac_address[temp];
}
 
Share this answer
 
Comments
chaiein 4-Apr-12 6:56am    
no it does not work


Error 23 error C2039: 'getlength' : is not a member of 'ATL::CStringT<BaseType,StringTraits>'

Error 24 error C2106: '=' : left operand must be l-value

Error 25 error C2106: '=' : left operand must be l-value
chaiein 5-Apr-12 6:36am    
I solved the problem of appending mac_address as in solution 3 .
How to replace a s[0] to 'p'
chaiein 5-Apr-12 7:58am    
SetAt(0,'p'); works thank you so much:)
C++
s+=mac_address;


appends to the end of the text.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900