0

Task: A text input as command-line argument is to be written to a file.

Issue: Some encoding conversion is happening somewhere, so the file doesn't contain the exact text which was input. e.g. s%3A_blabla becomes s:_blabla . (Some HTML kind of thing)

Environment: Visual Studio 2019, C++17

How is the command-line argument being supplied: Using Smart Command Line Arguments extension. Also read 'Edit 1'.

How is the input being processed:
I've tried two methods:

    static std::vector<const char*> args;
    for (size_t i = 0; i < __argc; i++)
    {
        args.push_back(__argv[i]);
    };
    ..........
    std::string filePath = "random.txt";
    std::string final_write(args[i+1]); //some value of i is chosen using a for loop
    std::cout<<final_write<<std::endl;//this will print "s:_blabla"
    std::ofstream out(filePath);
    out << final_write;

Second method:

     #include <atlstr.h>
     ........
     LPWSTR *szArgList;
     int argCount;

     szArgList = CommandLineToArgvW(GetCommandLineW(), &argCount);
     for (int i = 0; i < argCount; i++)
     {
        CString pathCString(szArgList[i]);
        std::cout<<pathCString<<std::endl; //Here also it prints "s:_blabla" for appropriate i
     }

What I know:

  1. I don't think cout is at fault, because in first case the file writing also give the same transformed output, and if I explicitly modify the string and add "%3A" to it, the cout is able to display that normally. I've not tried writing to file in the second case, as cout tells that something fishy has already happened during input. It might be the extension, or an encoding issue somewhere else, I'm not sure. Any help?

Edit 1: As suggested, I checked the behaviour after removing the command line extension. I took input from Properties->Debugging section->Command Arguments, and I still face the same issue. Disabled the extension too.

  • This looks like url encoding - https://stackoverflow.com/q/154536/4074081 there is a simplified implementation from scratch there and you can use UrlEscape on Windows. – dewaffled Jun 14 '21 at 07:53
  • @dewaffled I could do that, but I'm asking this for text in general, i.e. I want command line input to be preserved as it is. So e.g. the input is "%3A bA", currently I'll get ": bA", and if I do URL encoding I'll get "%3A%20bA" – Abhineet Pandey Jun 14 '21 at 07:58
  • So, you need the opposite? UrlUnescape? – dewaffled Jun 14 '21 at 08:00
  • 1
    Also, you should not need any additional encoding/decoding for the command line arguments. And if they come in this form specifically from the extension you mention then it is the extension bug. – dewaffled Jun 14 '21 at 08:02
  • 1
    I suspect that your "smart" extension is messing things up. – molbdnilo Jun 14 '21 at 08:03
  • It may be that. I wasnt able to use command line arguments using standard visual studio properties thing, this extension was interfering with that as well and clearing everything I guess. Will update here after removing the extension and checking. – Abhineet Pandey Jun 14 '21 at 08:06
  • @molbdnilo removing the extension didnt help, updated the question. – Abhineet Pandey Jun 14 '21 at 10:12

0 Answers0