-2

I'm calling a separate program, from my program, to handle a unit conversion. I get the return string just fine, but it doesn't split as I expect. I'm trying to split this (slightly edited) output:

50.05551390197077665789 50.60658280925650487347 0.00000000000000000000     

into a vector. Instead it splits into "50.05551390197077665789 50.60658280925650487347", "0.00000000000000000000".

I'm using this function to run the other program:

std::string Util::execute_cmd(const char* cmd) 
{
    std::array<char, 128> cmd_buffer;

    std::string result;

    std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose);

    if (!pipe) 
    {
        throw std::runtime_error("popen() failed!");
    }
    while (fgets(cmd_buffer.data(), cmd_buffer.size(), pipe.get()) != nullptr) 
    {
        result += cmd_buffer.data();
    }

    return result;
}

And I implement it here:

void convert_dktm2_to_geo_etrs98()
{
    std::string dktm2_pos1 = std::to_string(this->dktm2_position.at(0));
    std::string dktm2_pos2 = std::to_string(this->dktm2_position.at(1));

    std::string cmd = "echo " + dktm2_pos1 + " " + dktm2_pos2 + " | C:\\OSGeo4W64\\bin\\cs2cs epsg:4094 epsg:4258 -f \"%.20f\"";

    std::string result = Util::execute_cmd(cmd.c_str());

    std::vector<std::string> latlong = Util::separate_data(result, ' ');
}

I can print "result" and get the expected output, so it's calling the other program and getting a return value. My separate_data function has worked on everything else in my program. I even tried two other ways of splitting the string, but I got the same result. Here is my original implementation anyway

std::vector<std::string> Util::separate_data(std::string input, char delimiter)
{
    std::vector<std::string> data;
    std::stringstream ss(input);

    while (ss.good())
    {
        std::string entry;
        getline(ss, entry, delimiter);
        data.push_back(entry);
    }

    return data;
}

My best guess is that the first space isn't actually a space. Which I just confirmed by trying to change all spaces to commas in the string like this:

std::replace(result.begin(), result.end(), ' ', ',');

which returns this:

50.05551479305879780668 50.60658606200054876467,0.00000000000000000000,

I don't know what that character is. If I copy it it becomes a space. Can I run it through something to convert it to a space?

SproedAsfalt
  • 43
  • 1
  • 10
  • What is the hexadecimal value for the "space"? (Or decimal, should be just as easy) – AndyG Apr 27 '21 at 12:12
  • How do I check that? I can't easily isolate it in the program. And if I copy it it's just a space. – SproedAsfalt Apr 27 '21 at 12:13
  • 1
    print out the decimal values of each character in the string (e.g., `std::cout << static_cast(result[i])`). Then match up the index with the "space" You could also examine it during a debugging session. – AndyG Apr 27 '21 at 12:15
  • Is your goal to learn programming to transform the coordinates? If the later is your focus, I would recommend to use proj or gdal directly, unless there are other unknown constraints. Why? It makes your life with the transformation easier, faster and gives you more control. – mistapink Apr 27 '21 at 12:15
  • Does anything stop you from iterating over each character in the string, printing its ASCII value, so you can see what each character actually is? – Sam Varshavchik Apr 27 '21 at 12:16
  • @Sam: I just tried that and it says it's a 9. Does that mean it's a tab? – SproedAsfalt Apr 27 '21 at 12:26
  • It was a tab... Thanks for your help everyone. – SproedAsfalt Apr 27 '21 at 12:27
  • `while (ss.good())` suffers from the same problems as [`while (!stream.eof())`](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 27 '21 at 12:31

1 Answers1

0

The character was a tab. Thanks to the advice given in the post, I printed the ASCII values of each character in the string and could work from there.

std::vector<std::string> latlong = Util::separate_data(result, '\t')

Using this I can split the string where I was expecting.

SproedAsfalt
  • 43
  • 1
  • 10