Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::system

      From cppreference.com
      <cpp‎ |utility‎ |program
       
       
      Utilities library
       
       
      Defined in header<cstdlib>
      int system(constchar* command);

      Calls the host environment's command processor (e.g./bin/sh,cmd.exe) with the parametercommand. Returns an implementation-defined value (usually the value that the invoked program returns).

      Ifcommand is a null pointer, checks if the host environment has a command processor and returns a nonzero value if and only if the command processor exists.

      Contents

      [edit]Parameters

      command - character string identifying the command to be run in the command processor. If a null pointer is given, command processor is checked for existence

      [edit]Return value

      Implementation-defined value. Ifcommand is a null pointer, returns a nonzero value if and only if the command processor exists.

      [edit]Notes

      On POSIX systems, the return value can be decomposed usingWEXITSTATUS andWSTOPSIG.

      The related POSIX functionpopen makes the output generated bycommand available to the caller.

      An explicit flush ofstd::cout is also necessary before a call tostd::system, if the spawned process performs any screen I/O.

      [edit]Example

      Run this code
      #include <cstdlib>#include <fstream>#include <iostream> int main(){    std::system("ls -l >test.txt");// executes the UNIX command "ls -l >test.txt"std::cout<<std::ifstream("test.txt").rdbuf();}

      Possible output:

      total 16-rwxr-xr-x 1 2001 2000 8859 Sep 30 20:52 a.out-rw-rw-rw- 1 2001 2000  161 Sep 30 20:52 main.cpp-rw-r--r-- 1 2001 2000    0 Sep 30 20:52 test.txt

      [edit]See also

      C documentation forsystem
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/system&oldid=149861"

      [8]ページ先頭

      ©2009-2025 Movatter.jp