I am having problems with arguments in C++ console programs built under C++Builder 12 Community Edition.
If the executable is in a folder with spaces in its name, eg. "test dir", and I invoke it from a console session in a DIFFERENT folder, then the arguments extracted by my code are incorrect. If the executable file path has no spaces, everything works fine. If I run it within the IDE, it's OK, too. Code built under Visual Studio 22 also works OK.
CPPtest.cpp
#include <iostream.h>
int main(int argc, char* argv[])
{
cout << "argc: " << argc << "\n";
cout << "argv0: " << argv[0] << "\n";
cout << "argv1: " << argv[1] ;
return 0 ;`
}
The executable is located in a directory named "Misc projects", CWD is "C:\Junk":
C:\Junk>C:\"Misc projects"\CPPtest 3 8
argc: 2 ## INCORRECT: Should be 3
argv0: c:\Misc projects\CPPtest.exe
argv1: projects\CPPtest 3 8 # INCORRECT should be 8
Identical code when ran inside Visual Studio 22 performs correctly.
ParamCount()andParamStr()functions that you can use instead ofargc/argv. Internally, they parse the result ofGetCommandLineW().#include <iostream>instead of#include <iostream.h>