Page 1 of 1

wxString to double

Posted: Tue Jan 10, 2006 12:58 am
by chocobo_ff
Hi, this may sound like a stupid question, but is there a function for converting wxString to double, basically what I have is a string of numbers, separated by space, and I want to convert the string into an array of numbers (double), e.g. the string may be something like:

Code: Select all

wxString data = "1 2 3 4 5 6 7 8";
The problem I have is I don't know how to deal with it when there's an unknown number of doubles in the string (e.g. sometimes I could have 1, other times I'll have 10 say)

Thanks in advance for any help :)

Re: wxString to double

Posted: Tue Jan 10, 2006 1:05 am
by sethjackson
chocobo_ff wrote:Hi, this may sound like a stupid question, but is there a function for converting wxString to double,
.......
Yes there is, but not sure if it will help you.....

http://www.wxwidgets.org/manuals/2.6.2/ ... ngtodouble

Re: wxString to double

Posted: Tue Jan 10, 2006 1:08 am
by chocobo_ff
sethjackson wrote:Yes there is, but not sure if it will help you.....
Hi yes I know that function already... thanks for the help anyway :) actually I just found there's a function called AfterFirst that finds the first whitespace (' ') and take a sub string from there, so problem now solved :)

Re: wxString to double

Posted: Tue Jan 10, 2006 1:11 am
by sethjackson
chocobo_ff wrote:
sethjackson wrote:Yes there is, but not sure if it will help you.....
Hi yes I know that function already... thanks for the help anyway :) actually I just found there's a function called AfterFirst that finds the first whitespace (' ') and take a sub string from there, so problem now solved :)
Cool. 8)

Posted: Tue Jan 10, 2006 2:24 am
by Arakageeta1
You could also use std::stringstream and check the status flags to see if you've read in all the available numbers.

Posted: Tue Jan 10, 2006 6:04 am
by Cursor
What about use a wxStringTokenizer [1] to separate substrings and use wxString::ToDouble [2] to translate on double.


1 : http://www.wxwidgets.org/manuals/2.6.2/ ... gtokenizer
2 : http://www.wxwidgets.org/manuals/2.6.2/ ... ngtodouble

Posted: Wed Jan 11, 2006 10:29 pm
by ChrisBorgolte
Based on Arakageeta1 reply I would suggest something like:

std::string s = "1 2 3 4 5 6 8";
std::istringstream sst(s.c_str());
std::vector<double> V;
std::copy(std::istream_iterator<double>(sst), std::istream_iterator<double>(),
std::back_inserter(V));
//output to stdout:
std::copy( V.begin(), V.end(), std::ostream_iterator<double>(std::cout," ") );
std::cout << std::endl;

You can replace std::string s with wxString s.
This is just STL and has the advantage that you can use it even if you dont use wxWidgets.
Note that this method stops parsing at the given token if it can't translate the value to double. You will not get any information (exception) about it.

Posted: Wed Jan 11, 2006 10:58 pm
by Arakageeta1
I didn't know you could do that with std::copy. Nice.

Posted: Thu Jan 11, 2007 9:44 pm
by sobaka
Hi a have a similar problem, I need read from file some data. But I need to skip first lines in file and get amount of lines from “N=”, base on this get these lines. Then founded in number "E=" get the rest lines. Below you may see that I have in file.
And also that I do to get "N=" and numbers "E=".

===File===

Code: Select all


VARIABLES = "X","Y","Z" // < ------- well this I think understandable first column "x" second "y" third "z" this it is need to skip VARIABLES = "X","Y","Z"ZONE T="1",F=FEPOINT
 ZONE T="1",F=FEPOINT,N=   2.00000     ,E=   2.00000     ,ET=BRICK    // <---- N=   2.00000 this is how much lines "0.0000000D+00" like this numbers.
  0.0000000D+00  0.0000000D+00  0.0000000D+00
  0.1000000D+00  0.0000000D+00  0.0000000D+00
      1      2     11     10     28     29     38     37      2      1 // < --------------E=   2.00000 this how much such lines
      2      3     12     11     29     30     39     38      2      1
===Code===

Code: Select all


#include <fstream>
#include <string>
#include <iostream>
#include <strstream>

using namespace std;

inline double parseDouble(ifstream &str)
{
  string token;
  strstream convertor;
  double value;

  str >> token;
  convertor << token.replace(token.find('D'), 1, "E");
  convertor >> value;

  return value;
}

int main()
{

  ifstream src("MESHGL.dat");
  string token, line;
  int numPoints = -1, numElements = -1;
  int pos;

  do {
    src >> token;
    if (token.find("N=") != string::npos) src >> numPoints;
    if (token.find("E=") != string::npos) src >> numElements;
  } while (!src.eof() && (numPoints == -1 || numElements == -1));

  if (!src.eof())
    cout << numPoints << ", " << numElements << '\n';
  else
    cout << "Invalid source file format!\n";

  // skip end of line with "E="
  getline(src, line);

  double point[3];
  int elements[10];

  // start to read N-type lines
  while (numPoints--)
  {
    point[0] = parseDouble(src);
    point[1] = parseDouble(src);
    point[2] = parseDouble(src);

    cout << point[0] << "," << point[1] << "," << point[2] << "\n";

    // skip end of line (just in case of comments)
    getline(src, line);
  }

  // start to read E-type lines
  while (numElements--)
  {
    cout << "E=";
    for (int itemIndex=0; itemIndex<10; itemIndex ++)
    {
      src >> elements[itemIndex];
      cout << elements[itemIndex] << " ";
    }
    cout << "\n";
    // skip end of line (just in case of comments)
    getline(src, line);
  }
  system("PAUSE");
  return 0;
}

But it's gives me wrong numbers something like this.

Code: Select all

7.3,0.6,10
7.4,0.6,10
E=1 2 11 10 28 29 38 37 2 1
E=2 3 12 11 29 30 39 38 2 1

Posted: Fri Jan 12, 2007 5:31 pm
by sobaka
Is it so difficult? :roll: