NEWBIE: how to paste strings to make a command for system()

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

    NEWBIE: how to paste strings to make a command for system()

    Hi all.

    Having several strings how do i combine them to construct a command;
    lets say to run the date command.

    string str = "14/10/08 19:06:09";
    strDD = str.substr(0,2) ;
    strMM = str.substr(3,2) ;
    strAA = str.substr(6,2) ;
    strhh = str.substr(9,2) ;
    strmm = str.substr(12,2 );
    strss = str.substr(15,2 );

    strtarget<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
    " 2>&1 /dev/null;"<<endl;

    system(strtarge t);

    Not sure if the problem it's syntax in the strtarget line (59). when
    I compile i get this

    rtchk.cpp: In function `int main (int, char **)':
    rtchk.cpp:59: no match for `string & << const char[9]'

    Thanks in advance. really appreciate any advise.

  • Ian Collins

    #2
    Re: NEWBIE: how to paste strings to make a command for system()

    Atropo wrote:
    Hi all.
    >
    Having several strings how do i combine them to construct a command;
    lets say to run the date command.
    >
    string str = "14/10/08 19:06:09";
    strDD = str.substr(0,2) ;
    strMM = str.substr(3,2) ;
    strAA = str.substr(6,2) ;
    strhh = str.substr(9,2) ;
    strmm = str.substr(12,2 );
    strss = str.substr(15,2 );
    >
    strtarget<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
    " 2>&1 /dev/null;"<<endl;
    >
    system(strtarge t);
    >
    Not sure if the problem it's syntax in the strtarget line (59). when
    I compile i get this
    >
    rtchk.cpp: In function `int main (int, char **)':
    rtchk.cpp:59: no match for `string & << const char[9]'
    >
    Thanks in advance. really appreciate any advise.
    >
    std::string (I assume you are using std::string?) does not have an
    operator <<.

    You should be using an ostringstream.

    --
    Ian Collins

    Comment

    • Atropo

      #3
      Re: NEWBIE: how to paste strings to make a command for system()

      On 15 oct, 17:22, Ian Collins <ian-n...@hotmail.co mwrote:
      Atropo wrote:
      Hi all.
      >
      Having several strings how do i combine them to construct a command;
      lets say to run the date command.
      >
             string str   = "14/10/08 19:06:09";
         strDD = str.substr(0,2) ;
         strMM = str.substr(3,2) ;
         strAA = str.substr(6,2) ;
         strhh = str.substr(9,2) ;
         strmm = str.substr(12,2 );
         strss = str.substr(15,2 );
      >
      strtarget<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
      " 2>&1 /dev/null;"<<endl;
      >
              system(strtarge t);
      >
      Not sure if the problem it's syntax in the strtarget line (59).  when
      I compile i get this
      >
      rtchk.cpp: In function `int main (int, char **)':
      rtchk.cpp:59: no match for `string & << const char[9]'
      >
      Thanks in advance.  really appreciate any advise.
      >
      std::string (I assume you are using std::string?)  does not have an
      operator <<.
      >
      You should be using an ostringstream.
      >
      --
      Ian Collins- Ocultar texto de la cita -
      >
      - Mostrar texto de la cita -
      sorry Ian but I'm a really newbie. how should I use the
      ostringstream ??

      Comment

      • Ian Collins

        #4
        Re: NEWBIE: how to paste strings to make a command for system()

        Atropo wrote:
        >
        sorry Ian but I'm a really newbie. how should I use the
        ostringstream ??
        The same way as you use any other ostream, such as std::cout. In your
        case, replace the string you were trying to stream to with an ostringstream.

        Look up how to use the str() method to get the buffer's string.

        --
        Ian Collins

        Comment

        • Stephen Horne

          #5
          Re: NEWBIE: how to paste strings to make a command for system()

          On Wed, 15 Oct 2008 16:50:02 -0700 (PDT), Atropo <lxvasquez@gmai l.com>
          wrote:
          >sorry Ian but I'm a really newbie. how should I use the
          >ostringstrea m ??
          This is from memory so could be wrong, but should be close...


          std::ostringstr eam x;

          x << "here is some text" << 847 << "whatever";

          std::cout << x.str () << std::endl;

          The point is, std::ostringstr eam acts like any other stream in terms
          of how you stream data into it, but it buffers that data rather than
          outputing it. The str method (I think) provides access to the complete
          buffered string.

          Comment

          • Juha Nieminen

            #6
            Re: NEWBIE: how to paste strings to make a command for system()

            Ian Collins wrote:
            std::string (I assume you are using std::string?) does not have an
            operator <<.
            >
            You should be using an ostringstream.
            Given that the string is constructed exclusively with other strings,
            you could simply append them with the + operator, ie:

            strtarget = "date -u " + strMM + strDD + ...

            OTOH, IMO the whole approach is just wrong. The rule of thumb is that
            if you feel the need to use the 'system()' function you are doing things
            in the wrong way.

            Given that he is apparently trying to get a date in a specific format,
            there actually exists a *standard* function which does more or less the
            same thing as the unix 'date' command: strftime().

            Not only will it be more portable to use that, it will also be a lot
            more efficient.

            Comment

            • Juha Nieminen

              #7
              Re: NEWBIE: how to paste strings to make a command for system()

              Atropo wrote:
              strtarget<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
              " 2>&1 /dev/null;"<<endl;
              >
              system(strtarge t);
              You really want to look at the strftime() standard function rather
              than doing that ugly hack with the system() function. strftime() is not
              only more portable, it's also a lot more efficient.

              Comment

              • James Kanze

                #8
                Re: NEWBIE: how to paste strings to make a command for system()

                On Oct 16, 3:41 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                Ian Collins wrote:
                std::string (I assume you are using std::string?) does not
                have an operator <<.
                You should be using an ostringstream.
                Given that the string is constructed exclusively with other
                strings, you could simply append them with the + operator, ie:
                strtarget = "date -u " + strMM + strDD + ...
                Yes, but it's less flexible (because it requires everything to
                be a string), and less idiomatic.
                OTOH, IMO the whole approach is just wrong. The rule of thumb
                is that if you feel the need to use the 'system()' function
                you are doing things in the wrong way.
                Why? If portability isn't a concern (or in some cases, even if
                it is---I use common shell scripts and make files under Windows
                and Unix), and there is a system function which does exactly
                what you need done, why not use it, rather than reimplement it
                in your own code?
                Given that he is apparently trying to get a date in a specific
                format, there actually exists a *standard* function which does
                more or less the same thing as the unix 'date' command:
                strftime().
                That's true. The problem is that he already has a date in
                string format. All he is really trying to do is reorder fields
                and change the separator, and the fact that it is a date is more
                or less irrelevant. On the other hand, parsing his input into
                a tm structure, and then using strftime, would allow more
                flexibility with regards to the input format, e.g. 20/6/2008 for
                the date, for example.
                Not only will it be more portable to use that, it will also be
                a lot more efficient.
                In his case, using strftime would be less efficient, because it
                involves a conversion to int and back. But frankly, who cares.
                I'm pretty sure that all of the suggestions so far are efficient
                enough; it's not the sort of thing you're likely to find in a
                tight loop or a critical path.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • James Kanze

                  #9
                  Re: NEWBIE: how to paste strings to make a command for system()

                  On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                  Atropo wrote:
                  strtarget<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
                  " 2>&1 /dev/null;"<<endl;
                  system(strtarge t);
                  You really want to look at the strftime() standard function
                  rather than doing that ugly hack with the system() function.
                  The system function is being used to set the date. Neither
                  strftime() nor any other standard function will do that. (In
                  this case, using system is more portable than any of the
                  alternatives; it will work on any Unix system, and on Windows if
                  a Unix toolkit is accessible in your path.)
                  strftime() is not only more portable, it's also a lot more
                  efficient.
                  strftime() doesn't do what he wants, and any use of it here
                  would doubtlessly be less efficient, since it involves
                  conversions to int and back. (And what's all this business
                  about efficiency, anyway?)

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • Juha Nieminen

                    #10
                    Re: NEWBIE: how to paste strings to make a command for system()

                    James Kanze wrote:
                    >Not only will it be more portable to use that, it will also be
                    >a lot more efficient.
                    >
                    In his case, using strftime would be less efficient, because it
                    involves a conversion to int and back.
                    Are you seriously claiming that getting a proper struct tm instance
                    and calling strftime is less efficient than calling system(), which
                    usually spawns a command-line interpreter, parses the command and its
                    parameters, starts a new process with the program specified in the
                    command line, runs the program and returns the result? Especially since
                    the program in question ('date') probably itself also constructs a
                    struct tm instance and calls strftime.

                    Comment

                    • Juha Nieminen

                      #11
                      Re: NEWBIE: how to paste strings to make a command for system()

                      James Kanze wrote:
                      On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                      >Atropo wrote:
                      >>strtarget<<"d ate -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
                      >>" 2>&1 /dev/null;"<<endl;
                      >
                      >> system(strtarge t);
                      >
                      >You really want to look at the strftime() standard function
                      >rather than doing that ugly hack with the system() function.
                      >
                      The system function is being used to set the date.
                      At least here the -u parameter specifies that the date should be in
                      UTC. It says nothing about setting the date.
                      strftime() doesn't do what he wants, and any use of it here
                      would doubtlessly be less efficient, since it involves
                      conversions to int and back.
                      How can a call to strftime() be less efficient than spawning a
                      command-line interpreter and running the 'date' command, which itself
                      most probably calls strftime()?

                      Comment

                      • James Kanze

                        #12
                        Re: NEWBIE: how to paste strings to make a command for system()

                        On Oct 20, 2:20 pm, ytrem...@nyx.ny x.net (Yannick Tremblay) wrote:
                        In article <E5%Jk.67$1K... @read4.inet.fi> ,
                        Juha Nieminen  <nos...@thanks. invalidwrote:
                        Efficiency is unlikely to be important.  strftime would be of
                        no use because the date is in human readable format as
                        suggested above, not as a tm struct.  He could possibly use
                        strptime to get a tm then strftime to get a string back but
                        simple string manipulation would do the job at least as well
                        and probably faster.
                        Just a nit, but there is no strptime in standard C++. It's a
                        Unix extension.
                        You are not comparing strftime with system(), you are
                        comparing strftime+strpti me with string manipulation. (but
                        efficiency is very unlikely to be critical)
                        Compared to the time necessary to spawn the child process, very,
                        very unlikely. Depending on what his real specification is,
                        there can be an advantage in passing through the numeric values
                        and struct tm. It's more flexible, even if it is more work.
                        But you still have to parse the input (boost::regex seems the
                        best solution for that), then if you want numeric values, use
                        istringstream to convert the parsed substrings. (If strptime is
                        available, it might be slightly easier to use, but IMHO,
                        learning to use boost::regex for this sort of thing is worth the
                        effort.) Then you reformat the data you have to get what you
                        want; if it's all strings, you can use either ostringstream or
                        string concatenation; if you have numeric values, it has to be
                        ostringstream.

                        --
                        James Kanze (GABI Software) email:james.kan ze@gmail.com
                        Conseils en informatique orientée objet/
                        Beratung in objektorientier ter Datenverarbeitu ng
                        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                        Comment

                        • James Kanze

                          #13
                          Re: NEWBIE: how to paste strings to make a command for system()

                          On Oct 17, 1:58 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                          James Kanze wrote:
                          On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                          Atropo wrote:
                          >strtarget<<"da te -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
                          >" 2>&1 /dev/null;"<<endl;
                          >        system(strtarge t);
                          You really want to look at the strftime() standard function
                          rather than doing that ugly hack with the system() function.
                          The system function is being used to set the date.
                          At least here the -u parameter specifies that the date should
                          be in UTC. It says nothing about setting the date.
                          The command he generated is the command to set the system time
                          under Unix. And using system is probably the most portable way
                          of doing it; it will work under all Unix, where as all of the
                          other ways will only work under a specific Unix.
                          strftime() doesn't do what he wants, and any use of it here
                          would doubtlessly be less efficient, since it involves
                          conversions to int and back.
                          How can a call to strftime() be less efficient than spawning a
                          command-line interpreter and running the 'date' command, which
                          itself most probably calls strftime()?
                          Because he still needs to spawn a command line and execute an
                          external function to set the system time. All using strftime()
                          means is that he absolutely has to convert the numeric values in
                          his string to numbers (ints), and that he also has to program
                          around buffer length issues.

                          --
                          James Kanze (GABI Software) email:james.kan ze@gmail.com
                          Conseils en informatique orientée objet/
                          Beratung in objektorientier ter Datenverarbeitu ng
                          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                          Comment

                          Working...