Pablo Lopez
Pablo Lopez

Reputation: 37

Spaces in system() C++

I've tried with some solutions found in Stackoverflow, but i can't get it to work, i want to start a .LOG (.txt file) from C++, but the path folder containing it might have spaces, so when i try to start it, i get an error saying it cant find the file because the pah (containing spaces) is wrong, here is what my code looks like:

void Log (unsigned int Code,...)
{
char currdate[11] = {0};
SYSTEMTIME t;
GetLocalTime(&t);
sprintf(currdate, "%02d:%02d:%02d", t.wHour, t.wMinute, t.wSecond);

PROCESSENTRY32 pe32;
FILE* FileHwnd1;

FileHwnd1 = fopen("TEST.log","a+");
fprintf(FileHwnd1,"[%s] Code: %X\n",currdate,Code);
fclose(FileHwnd1);
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH);
char Path[50];

wsprintf(Path,"start %s\\AntiHack.log",buffer);
system(Path);//Here is where i get the containing spaces path error
}

Thanks.

Upvotes: 0

Views: 194

Answers (2)

ciphor
ciphor

Reputation: 8288

You can try:

wsprintf(Path,"start \"\" \"%s\"\\\AntiHack.log",buffer);

Upvotes: 0

WhozCraig
WhozCraig

Reputation: 66234

I would advise you avoid the system call entirely and do the process launch yourself.

  1. Use AssocQueryString() to find the associated process for your extension (in this case, .log)
  2. Setup and launch a CreateProcess() call to invoke, passing the appropriate command line.

there are other ways to do this, but as you're noticing now, going a round-about way will always have pitfalls. The above is spot-on with how Explorer.exe launches the associated process for an extension.

Upvotes: 2

Related Questions