0

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.

Rndm
  • 6,710
  • 7
  • 39
  • 58

2 Answers2

2

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.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
0

You can try:

wsprintf(Path,"start \"\" \"%s\"\\\AntiHack.log",buffer);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
ciphor
  • 8,018
  • 11
  • 53
  • 70
  • I think you mean `start \"%s\\AntiHack.log\"`. – chris Sep 19 '12 at 03:50
  • 1
    True, but we both missed that you need `start "" "whatever.lol"` if quotations are needed or it will just open a new console window with that as the title (at least in Windows, not sure otherwise). I noticed you fixed the \A issue as well. – chris Sep 19 '12 at 03:54
  • Now it seems to be getting the path correctly, but it happens what Chris said, i only get a new CMD with the path on the title, how should i fix that?. Thanks. – Pablo Lopez Sep 19 '12 at 03:58
  • @PabloLopez, You need a `\"\"` after `start`. – chris Sep 19 '12 at 04:01
  • Ok, i've tried this: wsprintf(Path,"start \"\"\"%s\\AntiHack.log\"",buffer); But it just opens the CMD with the path on the title – Pablo Lopez Sep 19 '12 at 04:10
  • @Pablo: You need a space between the pair of quotes and the quoted command. – Ben Voigt Sep 19 '12 at 04:28