Click here to Skip to main content
14,870,204 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]
   
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.
chaiein 5-Apr-12 6:31am
   
if(s.Left[1]==_T("L"))
{
s+=_T("P");
...
}

error C3867: 'ATL::CStringT<BaseType,StringTraits>::Left': function call missing argument list; use '&ATL::CStringT<BaseType,StringTraits>::Left' to create a pointer to member
error C2109: subscript requires array or pointer type
CPallini 5-Apr-12 6:46am
   
Is s.Left(1)...
That is ROUND braces. Please check my code.
chaiein 5-Apr-12 7:54am
   
sorry i had written wrong.now it works thank you :)
CPallini 5-Apr-12 8:11am
   
You are welcome.
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];
}
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.
   

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