Menu

Typo in example?

Help
2023-02-21
2024-08-24
  • Oscar Kramer

    Oscar Kramer - 2023-02-21

    From https://pstreams.sourceforge.net/ :

    // remove some files, capturing any error messages
    std::vector<std::string> argv;
    std::vector<std::string> errors;
    argv.push_back("rm");
    argv.push_back("./foo.txt");
    argv.push_back("./bar.html");
    redi::ipstream in("rm", argv, pstreambuf::pstderr);
    std::string errmsg;
    while (std::getline(in, errmsg)) {
        errors.push_back(errmsg);
    }
    

    Should "rm" be pushed onto the vector? It is provided again in the ipstream declaration. Why twice?

     
  • Jonathan Wakely

    Jonathan Wakely - 2024-08-24

    The example is correct. The first argument to the ipstream constructor is the name of the executable to run (which will be looked up in PATH). The argv strings are passed to the new process as its argv[] array. By convention, argv[0] is the name of the executable that was run, but it doesn't have to be. You can actually pass some other string in argv[0].

    So it's correct that there are two "rm" strings. One is used by the OS to find the program to run, and the other is passed to that program as its argv[0] argument.

     

Log in to post a comment.