Home > Software engineering >  MFC example c
MFC example c

Time:09-21

Really don't understand c + +, this example how to do!

Char * _stdcall stringCombo (char * str1, char * str2)
{
Char * str3="upstring=";

Char * str4="& amp; UpFullFileName=";

Char combo=new char []
Sprintf_s (combo, sizeof (combo), "% s, % s, % s, % s", str1, str2, str3, str4);


Return combo
}

//combo need to turn the str1 str2, str3, str4 pieced together, can you tell me how?

Not write their own examples, could you please tell me what to do, with c + + MFC VS2015

CodePudding user response:

Should someone oh,

CodePudding user response:

reference 1st floor ah_2056 response:
should someone oh,

The building Lord, the following code for reference ~ ~ ~ ~
 
Char * _stdcall stringCombo (char * str1, char * str2)
{
Char * str3="upstring=";
Char * str4="& amp; UpFullFileName=";

Int nLen=strlen (str3);
NLen +=strlen (str4);

If (str1)
NLen +=strlen (str1);

If (str2)
NLen +=strlen (str2);

Char * combo=new char [nLen + 1];
If (combo)
{
Memset (combo, 0, nLen + 1);
Sprintf_s (combo, nLen + 1, "% s % s % s % s", str3, str1, str4, str2);
}

Return combo.
}


Int main ()
{
Char * pRetChar=stringCombo (" hello ", "the word");
If (pRetChar)
{
Printf_s (pRetChar);
The delete [] pRetChar;
}
return 0;
}

CodePudding user response:

Use snprintf,

Since using the MFC project, why not use cstrings or string, they are overloaded +, string handling very convenient

CodePudding user response:

MFC using cstrings inside dealing with string link is very convenient, nearly overloaded methods are good, you just need to call

CodePudding user response:

refer to the second floor SGHCPT response:
Quote: refer to 1st floor ah_2056 response:

Should someone oh,

The building Lord, the following code for reference ~ ~ ~ ~
 
Char * _stdcall stringCombo (char * str1, char * str2)
{
Char * str3="upstring=";
Char * str4="& amp; UpFullFileName=";

Int nLen=strlen (str3);
NLen +=strlen (str4);

If (str1)
NLen +=strlen (str1);

If (str2)
NLen +=strlen (str2);

Char * combo=new char [nLen + 1];
If (combo)
{
Memset (combo, 0, nLen + 1);
Sprintf_s (combo, nLen + 1, "% s % s % s % s", str3, str1, str4, str2);
}

Return combo.
}


Int main ()
{
Char * pRetChar=stringCombo (" hello ", "the word");
If (pRetChar)
{
Printf_s (pRetChar);
The delete [] pRetChar;
}
return 0;
}
666 brothers

CodePudding user response:

Introducing more convenient cstrings
Cstrings stringCombo (const & amp; Cstrings str1, const & amp; Cstrings str2)
{
Cstrings str3="upstring=";
Cstrings str4="& amp; UpFullFileName=";

Return str3 + str1 + str4 + str2;
}


Int main ()
{
Cstrings pRetChar=stringCombo (" hello ", "the word");
TRACE (pRetChar);
return 0;
}

CodePudding user response:

Positive solution to the second floor, new char [] this kind of thing, you will crash program, if you do not specific requirement char * format, using cstrings more convenient, can be directly

CodePudding user response:

MFC is using cstrings class.
 cstrings _stdcall stringCombo (cstrings * str1, cstrings * str2) 
{
Cstrings str3="upstring=";

Cstrings str4="& amp; UpFullFileName=";

Cstrings combo=str3 + str1 + str4 + str2;


Return combo.
}

CodePudding user response:

Cstrings class directly +=more simple and quick operation can

Cstrings Str, Str1, Str2 Str3, Str4;

Str +=(Str1 + Str2 + + Str4 Str3);

CodePudding user response:

 char * _stdcall stringCombo (char * str1, char * str2) 
{
Char * str3="upstring=";
Char * str4="& amp; UpFullFileName=";
Int L=0;
Char combo=new char [1024];//use a larger buffer, to ensure that adequate
Char * p=combo;

L=strlen (str3);
Memcpy (px, str3, L);
Px +=L;

L=strlen (str1);
Memcpy (px, str1, L);
Px [L]="; ";//to separate the
Px++;

L=strlen (str4);
Memcpy (px, str4, L);
Px +=L;

L=strlen (str2);
Memcpy (px, str2, L);
Px [L]=0;

Return combo
}

CodePudding user response:

Char combo=new char [ size ]//the size must specify the size

CodePudding user response:

Char combo=new char [size];//the size must be specified size
And it should be

Char * combo=new char [size];
  • Related