std::system(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::system(3) πŸ’–

okay so this is the std::system(3) page from the C++ Standard Libary manual, and bestie, it's giving "call the host environment's command processor" energy. no cap, we're keeping every single detail. 🫢


✨ NAME ✨

  • std::system β€” literally std::system, the main character. πŸ‘‘

🌸 Synopsis 🌸

Defined in header <cstdlib>:

int system( const char* command );

Okay so what it does: it calls the host environment's command processor (think /bin/sh, cmd.exe) with the parameter command. It returns an implementation-defined value β€” usually the value that the invoked program returns. πŸ’…

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

πŸŽ€ Parameters πŸŽ€

  • command β€” a character string identifying the command to be run in the command processor. If a null pointer is given, the command processor is checked for existence. πŸ¦‹

πŸ’– Return value πŸ’–

Implementation-defined value, bestie. If command is a null pointer, it returns a nonzero value if and only if the command processor exists. Periodt. πŸ’€

πŸ¦‹ Notes πŸ¦‹

  • On POSIX systems, the return value can be decomposed using WEXITSTATUS and WSTOPSIG. 🐚
  • The related POSIX function popen makes the output generated by command available to the caller. πŸ“¬
  • An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O. 😭

⭐ Example ⭐

Run this code, bestie:

// 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

πŸ‘‘ See also πŸ‘‘

C documentation for system β€” that's the related C docs, go sip that tea too. 🍡

πŸ’– source: http://cppreference.com Β· version/date: 2024.06.10 Β· xoxo, your Unix bestie πŸ’–

πŸ“¦ where this page comes from