Using "wc" inside C program

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

    Using "wc" inside C program

    Or any other standar unix program..
    I want to pass something to a pipe for wc (to count words) and read
    the result into a variable..

    For now I put an intermediate result into a file and then open the
    file and read it..
    thanks
    bye
  • Malcolm McLean

    #2
    Re: Using "wc&quo t; inside C program


    "Snaggy" <l.cioria@gmail .comwrote in message news
    Or any other standar unix program..
    I want to pass something to a pipe for wc (to count words) and read
    the result into a variable..
    >
    For now I put an intermediate result into a file and then open the
    file and read it..
    thanks
    >
    system() is the ANSI -standard way of calling an external program,
    However Unix machines usually offer a fucntion called popen() for pipe open,
    which may be what you are looking for.
    However since it is non-standard current topicality conventions state that I
    can't tell you much about it.

    --
    Free games and programming goodies.


    Comment

    • Richard Tobin

      #3
      Re: Using &quot;wc&quo t; inside C program

      In article <525be7b5-f6d1-4826-9668-cd0909e65446@1g 2000prd.googleg roups.com>,
      Snaggy <l.cioria@gmail .comwrote:
      >Or any other standar unix program..
      >I want to pass something to a pipe for wc (to count words) and read
      >the result into a variable..
      >
      >For now I put an intermediate result into a file and then open the
      >file and read it..
      Reading and writing to another program through pipes is, in general, a
      recipe for deadlock. In the case of "wc" this wouldn't be a problem,
      but if you were using something like "tr" it would. To make it work
      requires some kind of asynchronous (or select()ed) i/o, multiple
      threads, or arbitrary buffering in the operating system. Consequently
      the unix popen() call only provides for you to either read or write,
      not both.

      For this simple case, your solution of using a temporary file is
      probably the easiest, requiring little unix-specific knowledge.

      -- Richard
      --
      Please remember to mention me / in tapes you leave behind.

      Comment

      • Richard Tobin

        #4
        Re: Using &quot;wc&quo t; inside C program

        In article <u-Kdnb92xZu39L3Un Z2dnUVZ8vKdnZ2d @bt.com>,
        Malcolm McLean <regniztar@btin ternet.comwrote :
        >system() is the ANSI -standard way of calling an external program,
        >However Unix machines usually offer a fucntion called popen() for pipe open,
        >which may be what you are looking for.
        >However since it is non-standard current topicality conventions state that I
        >can't tell you much about it.
        system() requires almost as much non-C-standardness as popen(), since
        you still need how to get a legal temporary file name, and the syntax
        for redirection.

        In any case, the OP's description suggests that he is already using
        popen(). You can't do what he's asking for with it.

        -- Richard
        --
        Please remember to mention me / in tapes you leave behind.

        Comment

        • Snaggy

          #5
          Re: Using &quot;wc&quo t; inside C program

          On Nov 16, 7:40 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
          "Snaggy" <l.cio...@gmail .comwrote in message news
          Or any other standar unix program..
          I want to pass something to a pipe for wc (to count words) and read
          the result into a variable..
          >
          For now I put an intermediate result into a file and then open the
          file and read it..
          thanks
          >
          system() is the ANSI -standard way of calling an external program,
          However Unix machines usually offer a fucntion called popen() for pipe open,
          which may be what you are looking for.
          However since it is non-standard current topicality conventions state that I
          can't tell you much about it.
          >
          --
          Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
          popen's perfect
          thanks

          Comment

          • Nate Eldredge

            #6
            Re: Using &quot;wc&quo t; inside C program

            Snaggy <l.cioria@gmail .comwrites:
            Or any other standar unix program..
            I want to pass something to a pipe for wc (to count words) and read
            the result into a variable..
            >
            For now I put an intermediate result into a file and then open the
            file and read it..
            For something as simple as wc, it's probably faster, easier, and more
            portable just to do the word count yourself. Here's one possibility,
            which I hereby release into the public domain.

            #include <stddef.h>
            #include <ctype.h>

            size_t count_words(con st char *s) {
            int inword = 0;
            size_t words = 0;
            for ( ; *s; s++) {
            if (!inword && !isspace(*s)) {
            /* started a word */
            inword = 1;
            words++;
            } else if (inword && isspace(*s)) {
            inword = 0;
            }
            }
            return words;
            }

            Counting lines and characters is of course even easier.

            Comment

            Working...