0

I understand how to pass arguments between PHP and C++ and what (int argc, char** argv) is, but in terms of visible examples given it is always to do with integers or strings.

I want to send across vectors and booleans. As answered in a previous question of mine, serialization is an option, but I've also been told that this isn't necessary. Either way, how to read the arguments out from char** argv and into their corresponding variable holders (i.e. a std::vector<std::string>) remains a mystery.

Can someone please help me out here with either an example, or a link to an example that doesn't involve integers or strings?

Community
  • 1
  • 1
MLMLTL
  • 1,519
  • 5
  • 21
  • 35
  • Your boolean will be transfered as an integer. From the previous answer that was given to you, I would go for a filename argument(putting your array content in the command line would be dirty) which will contain the vector elements delimited by a special character. Your C++ program will have to transform the formatted file content into an std::vector . – Answers_Seeker Jun 30 '15 at 14:38
  • 1
    What are you unclear about? How to convert `char **argv` to a vector of strings, or are you looking for a way to change `int main (int argc, char **argv)` into some homegrown version like `int main (std::vector my_vec); ` because the latter is not an option – Elias Van Ootegem Jun 30 '15 at 14:41

1 Answers1

1

To read char ** argv into std::vector<std::string>

int main(int argc, char ** argv) {
    ++argv, --argc;    // To skip the first argument, i.e. filename

    std::vector<std::string> vec(argv, argv + argc);
    ...
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • but does that only looks to work when all the arguments belong to the one `vector`, rather than have say the the first 2 arguments as integers, then the third being a vector, and then "forth" being a string, right? – MLMLTL Jun 30 '15 at 14:40
  • @MLMLTL if you know the numbers, divide it accordingly – Shreevardhan Jun 30 '15 at 14:41
  • then it cant be dynamic – MLMLTL Jun 30 '15 at 14:41
  • @MLMLTL: It can, if you use `getopt` and pass the arguments using flags, or if you check each value in `argv` for numerics, strings and other markers that would help you sort out the one from the next – Elias Van Ootegem Jun 30 '15 at 14:42
  • @MLMLTL if required parse the arguments using functions like `getopt()` or other libraries, and then transform to vectors or booleans – Shreevardhan Jun 30 '15 at 14:43
  • Could you provide an example please? – MLMLTL Jun 30 '15 at 14:47
  • 1
    @MLMLTL Why not read it yourself http://stackoverflow.com/questions/2219562/using-getopt-to-parse-program-arguments-in-c, and http://code.runnable.com/UqvNM9NFP0gtAAAM/command-line-arguments-with-options-in-c%2B%2B – Shreevardhan Jun 30 '15 at 14:49
  • 1
    @MLMLTL: Come on.. asking for help is all well and good, but basic google-usage isn't that hard – Elias Van Ootegem Jun 30 '15 at 14:49
  • @EliasVanOotegem Valid point, but occasionally people have links saved that aren't you're run of the mill results, and although yes you are correct, other people know what certain terms to trigger the best results too – MLMLTL Jun 30 '15 at 14:53
  • @Shreevardhan in terms of passing large numbers (i.e. thousands) of strings across to a program, how would you say the best way to do this would be? – MLMLTL Jul 06 '15 at 09:52
  • @MLMLTL It depends upon the case. Generally File I/O is considered best useful to pass large amount of input to a program. – Shreevardhan Jul 06 '15 at 10:01
  • So on the `PHP` side, have it write the contents of a database table to file, save that, and then pass the file location across so to be read in the `C++` program, [kind of like this](http://stackoverflow.com/questions/6574927/exporting-mysql-table-to-txt-or-doc-file-using-php)? – MLMLTL Jul 06 '15 at 10:19
  • 1
    @MLMLTL You can even try accessing the database itself from C++ using some efficient API's to save the file I/O time but it is primarily opinion based. – Shreevardhan Jul 06 '15 at 10:20