Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to combine two strings in order to complete a path to a file, but I encountered 100s of errors trying to accomplish that, I am a new C++ programmer, because I program mainly on VB.net. See, VB.net has limitations so I decided to modify a program that is written in C++ because it cannot be converted to Vb.net.
Here are the codes of a program called ClamAVGUI, it is a old GUI for the antivirus programm ClamWin. this is the CPP file of the project, it is just an exceprt. I am trying to do is this, The program has checkboxes, each
checkboxes has a command, like for example, If i checked a box named, "Log the Scan" the program will append to the CLI exe, the ff. clamscan.exe "--log=PATH TO LOG FILE", this is what I am trying to d now,
but I need to find the program on where it is, for example finding its %HOMEDRIVE% where it is installed, for ex. at drive "C:\", then I will add the ff. commands to make the program work,
GetEnvironmentValue("homedrive") += """\\ClamAV\\Logs\\logfile.log", but I cannot do this in C++. Please help me, and guide me, I've trying this for hours and still no conclusion.
I don't know if this is a MFC app, I don't how to idenfity C++ program types, please guide me. Thank You.



C++
std::string cmdArg;
std::string strcmd1 = "--log=";
std::string strcmd2 =  GetEnvironmentValue("HOMEDRIVE");
std::string strcmd3 = "\CLamAV\logs\Scan.log";
cmdArg.append(strcmd1);
cmsArg.append(strcmd2);
cmdArg.append(strcmd3); //ERR here


const char * GetEnvironmentValue(const char * name)
{
    const DWORD buffSize = 65535;
    static char buffer[buffSize];
    if (GetEnvironmentVariableA(name, buffer, buffSize))
    {
        return buffer;
    }
    else
    {
        return 0;
    }
}

static Option options[] =
		
{

    { "--recursive",             IDC_RECURSE         },
    { "--remove",       IDC_RADIO1          },
    { "-i",             IDC_ONLYINFECTED    },

    { "--no-summary ",      IDC_NOSUMMARY          },
    { "--bell",        IDC_BEEP            },
	{ " ",        IDC_REPORT            },
	{ "--database="".\\db""",        IDC_db            }, // I need to do it here two, ex.  "--database=" & GetEnvironmentValue("HOMEDRIVE") += "\CLamAV\database" still no luck
	{"--log=" +=  GetEnvironmentValue("HOMEDRIVE") += "\CLamAV\logs\Scan.log", IDC_LOGSCAN}, //CAN'T GET THIS TO WORK... Tried 100 Times... I just need to add the proper file location so there won't be any error but, it throws an error when compiling.
	/*In VB.net If I am going to combine two strings it is easy as:   
	Dim pathoffolder as string = "C:\windows"
	Dim pathoffile as sting = "\cmd.exe"
	Msgbox(pathoffolder & pathoffile)
	Process.Start(pathoffolder & pathoffile, "arguments")
	I don't know how to do it in C++, VC++.*/
	
	  { NULL, 0 }
	};



-------------------------------UPDATED----------------------------------
Okay I managed to combine two strings by using the ff code:


C#
char testString1[200];
char testString2[100];
            strcpy(testString2,buffer); //Second String
            strcpy(testString1,"--tempdir="""); // first string
            strcat(testString1, testString2);
//combined string would be testString1

static Option options[] =
		
{
//...
};


Then another problem arises, ERROR: IntelliSense: this declaration has no storage class or type specifier

I cannot paste it outside, but I can if it is in a another function.
Posted
Updated 29-Aug-14 23:33pm
v3

1 solution

C++
{"--log=" +=  GetEnvironmentValue("HOMEDRIVE") += "\CLamAV\logs\Scan.log", IDC_LOGSCAN}, //CAN'T GET THIS TO WORK

That will never work, you cannot use runtime expressions (+=) or functions (GetEnvironmentValue) at compile time. You need to learn to use the string template class[^] to build your values when the program actually runs. If you find that difficult, you can always use the old C runtime sprintf function[^].
 
Share this answer
 
Comments
layapupam 30-Aug-14 5:34am    
This code worked but a new problem is:

char testString1[200];
char testString2[100];
strcpy(testString2,buffer); //Second String
strcpy(testString1,"--tempdir="""); // first string
strcat(testString1, testString2);
//combined string would be testString1

static Option options[] =

{
//...
};


Then another problem arises, ERROR: IntelliSense: this declaration has no storage class or type specifier

I cannot paste it outside, but I can if it is in a another function.
Richard MacCutchan 30-Aug-14 5:46am    
I already explained that you cannot generate compile time values at run time.

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