Messy strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • saneman

    Messy strings

    I am trying to call gnuplot from C++. I have to make a system call
    containing the command and path to a data file:

    std::string path_to_data = "c:\\data.t xt";
    std::string gnuplot = "wgnuplot.e xe";
    std::string gnuplot_call = gnuplot + " " + path_to_data;
    system(&gnuplot _call[0]);

    But system wants a char*. Is there anyway to make the above more
    simple/readable?


  • Ian Collins

    #2
    Re: Messy strings

    saneman wrote:
    I am trying to call gnuplot from C++. I have to make a system call
    containing the command and path to a data file:
    >
    std::string path_to_data = "c:\\data.t xt";
    std::string gnuplot = "wgnuplot.e xe";
    std::string gnuplot_call = gnuplot + " " + path_to_data;
    system(&gnuplot _call[0]);
    >
    But system wants a char*. Is there anyway to make the above more
    simple/readable?
    >
    system() expects a const char*, so you should use gnuplot_call.c_ str().

    --
    Ian Collins.

    Comment

    • blargg

      #3
      Re: Messy strings

      In article <6g4vj2Fdrk1fU1 @mid.individual .net>, Ian Collins
      <ian-news@hotmail.co mwrote:
      saneman wrote:
      I am trying to call gnuplot from C++. I have to make a system call
      containing the command and path to a data file:

      std::string path_to_data = "c:\\data.t xt";
      std::string gnuplot = "wgnuplot.e xe";
      std::string gnuplot_call = gnuplot + " " + path_to_data;
      system(&gnuplot _call[0]);

      But system wants a char*. Is there anyway to make the above more
      simple/readable?
      >
      system() expects a const char*, so you should use gnuplot_call.c_ str().
      More importantly, it wants a nul-terminated string, which &gnuplot_cal l[0]
      is not; only by bad luck would the undefined access past the end be a nul
      character. c_str() is the only correct solution of the two above.

      Comment

      Working...