Executing a process within c

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

    Executing a process within c

    How do I programatically run a dos command like 'dir' (I assume this will be
    the same way as running normal files, which I'd like to know how to do too).
    Also, will the output from this go into stdout?

    Is there any better way to get a list of files in a directory than parsing a
    'dir' ?

    Thanks,
    J


  • Ben Pfaff

    #2
    Re: Executing a process within c

    "Jeremy" <jokerdru@n0sp4 mhotmail.com> writes:
    [color=blue]
    > How do I programatically run a dos command like 'dir' (I assume this will be
    > the same way as running normal files, which I'd like to know how to do too).[/color]

    This is a FAQ.

    19.27: How can I invoke another program (a standalone executable,
    or an operating system command) from within a C program?

    A: Use the library function system(), which does exactly that.
    Note that system's return value is at best the command's exit
    status (although even that is not guaranteed), and usually has
    nothing to do with the output of the command. Note also that
    system() accepts a single string representing the command to be
    invoked; if you need to build up a complex command line, you can
    use sprintf(). See also question 19.30.

    References: K&R1 Sec. 7.9 p. 157; K&R2 Sec. 7.8.4 p. 167,
    Sec. B6 p. 253; ISO Sec. 7.10.4.5; H&S Sec. 19.2 p. 407; PCS
    Sec. 11 p. 179.
    [color=blue]
    > Also, will the output from this go into stdout?[/color]

    Normally.
    [color=blue]
    > Is there any better way to get a list of files in a directory than parsing a
    > 'dir' ?[/color]

    This is also a FAQ.

    19.20: How can I read a directory in a C program?

    A: See if you can use the opendir() and readdir() functions, which
    are part of the POSIX standard and are available on most Unix
    variants. Implementations also exist for MS-DOS, VMS, and other
    systems. (MS-DOS also has FINDFIRST and FINDNEXT routines which
    do essentially the same thing.) readdir() only returns file
    names; if you need more information about the file, try calling
    stat(). To match filenames to some wildcard pattern, see
    question 13.7.

    References: K&R2 Sec. 8.6 pp. 179-184; PCS Sec. 13 pp. 230-1;
    POSIX Sec. 5.1; Schumacher, ed., _Software Solutions in C_
    Sec. 8.
    --
    "Large amounts of money tend to quench any scruples I might be having."
    -- Stephan Wilms

    Comment

    • Keith Thompson

      #3
      Re: Executing a process within c

      "Jeremy" <jokerdru@n0sp4 mhotmail.com> writes:[color=blue]
      > How do I programatically run a dos command like 'dir' (I assume this will be
      > the same way as running normal files, which I'd like to know how to do too).
      > Also, will the output from this go into stdout?[/color]

      Use the system() function, e.g.:

      system("dir");

      The output of the "dir" command will normally go to stdout.
      [color=blue]
      > Is there any better way to get a list of files in a directory than parsing a
      > 'dir' ?[/color]

      There are a number of better ways, but none of them are portable (the
      C standard has no concept of directories). You'll need to ask in a
      newsgroup that's specific to your operating system.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • Jeremy

        #4
        Re: Executing a process within c

        Thanks, most helpful, where can I get my hands on the whole FAQ?

        J
        "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
        news:87acy76790 .fsf@benpfaff.o rg...[color=blue]
        > "Jeremy" <jokerdru@n0sp4 mhotmail.com> writes:
        >[color=green]
        > > How do I programatically run a dos command like 'dir' (I assume this[/color][/color]
        will be[color=blue][color=green]
        > > the same way as running normal files, which I'd like to know how to do[/color][/color]
        too).[color=blue]
        >
        > This is a FAQ.
        >
        > 19.27: How can I invoke another program (a standalone executable,
        > or an operating system command) from within a C program?
        >
        > A: Use the library function system(), which does exactly that.
        > Note that system's return value is at best the command's exit
        > status (although even that is not guaranteed), and usually has
        > nothing to do with the output of the command. Note also that
        > system() accepts a single string representing the command to be
        > invoked; if you need to build up a complex command line, you can
        > use sprintf(). See also question 19.30.
        >
        > References: K&R1 Sec. 7.9 p. 157; K&R2 Sec. 7.8.4 p. 167,
        > Sec. B6 p. 253; ISO Sec. 7.10.4.5; H&S Sec. 19.2 p. 407; PCS
        > Sec. 11 p. 179.
        >[color=green]
        > > Also, will the output from this go into stdout?[/color]
        >
        > Normally.
        >[color=green]
        > > Is there any better way to get a list of files in a directory than[/color][/color]
        parsing a[color=blue][color=green]
        > > 'dir' ?[/color]
        >
        > This is also a FAQ.
        >
        > 19.20: How can I read a directory in a C program?
        >
        > A: See if you can use the opendir() and readdir() functions, which
        > are part of the POSIX standard and are available on most Unix
        > variants. Implementations also exist for MS-DOS, VMS, and other
        > systems. (MS-DOS also has FINDFIRST and FINDNEXT routines which
        > do essentially the same thing.) readdir() only returns file
        > names; if you need more information about the file, try calling
        > stat(). To match filenames to some wildcard pattern, see
        > question 13.7.
        >
        > References: K&R2 Sec. 8.6 pp. 179-184; PCS Sec. 13 pp. 230-1;
        > POSIX Sec. 5.1; Schumacher, ed., _Software Solutions in C_
        > Sec. 8.
        > --
        > "Large amounts of money tend to quench any scruples I might be having."
        > -- Stephan Wilms[/color]


        Comment

        • Ben Pfaff

          #5
          Re: Executing a process within c

          "Jeremy" <jokerdru@n0sp4 mhotmail.com> writes:
          [color=blue]
          > Thanks, most helpful, where can I get my hands on the whole FAQ?[/color]

          What, is Google down?
          --
          Ben Pfaff
          email: blp@cs.stanford .edu
          web: http://benpfaff.org

          Comment

          • Keith Thompson

            #6
            Re: Executing a process within c

            "Jeremy" <jokerdru@n0sp4 mhotmail.com> writes:[color=blue]
            > Thanks, most helpful, where can I get my hands on the whole FAQ?[/color]

            Google "C FAQ"; it's the first link.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Randy Howard

              #7
              Re: Executing a process within c

              In article <40f0a5ff@news. comindico.com.a u>, jokerdru@n0sp4m hotmail.com says...[color=blue]
              > Thanks, most helpful, where can I get my hands on the whole FAQ?[/color]

              This link might be exactly what you need:

              Just Fucking Google It! Home Page -- politely* tell people to Google things first


              Comment

              Working...