Home > Net >  How to append data to const whar_t*
How to append data to const whar_t*

Time:11-21

I am building a project based on STM32CubeProgrammer API. The filepath is is done like this and you have to input the filename in the code manually.

    /* Download File   verification */
#ifdef _WIN32
    const wchar_t* filePath = L"../test file/filename.hex";
#else
    const wchar_t* filePath = L"../api/test file/filename.hex";
#endif

I want the program to show a list of available .hex files, ask for a corresponding number and then append the correct filename to the filePath. The goal is to ask for minimal input from user and keep it as simple as possible.

filePath should remain as const wchar_t*.

I wasn't able to find anything working on Google and I am not even sure how and what to search.

How can this be done?

CodePudding user response:

I'm not sure why you need, or think that you need, const wchar_t* pointer. But instead of trying to "workaround" C standard and compilers by appending filename to path you can do it "other way around", i.e. instead of appending you can show only filenames, i.e. something like that:

const wchar_t *file_paths[] = {
  "some path/filename1.hex",
  "some path/filename2.hex",
...
};

const wchar_t* select_path() {
  for (const wchar_t *path : file_paths) {
    const wchar_t* filename = get_file_name_from_path(path);
    out_file_name(filename);
  }
  return file_paths[input_number()];
}

CodePudding user response:

Working solution, thanks to @Someprogrammerdude. User input not yet implemented.

        std::wstring projects[] = { L"data.hex", L"blinky.hex" };
        int projectNr = 0;

        std::wstring file = L"../test file/"   projects[projectNr];


#ifdef _WIN32
        const wchar_t* filePath = file.c_str();
#else
        const wchar_t* filePath = L"../api/test file/";
#endif
  • Related