Is there a way to use the unix 'time' command in C++ and store each of its outputs in a variable?
EDIT: If there isn't a way, then what about calling time in a bash script and storing the returned values some way?
Thank you!
Supposing you are on UNIX, then the C++ standard std::system function will behave as defined in POSIX, that is, execute a command as with sh. Before doing this, you can connect your own stdin and stdout to a local pipe by first using dup to create aliases of STDIN_FILENO and STDOUT_FILENO, then close the aforementioned file descriptors, then pipe to open a pipe on the newly freed descriptors.
Then you can interact with std::cin and std::cout. Well, it would be a good idea to flush the C++ interface before beginning.
This isn't all really a good idea, though. It should be simpler to use the POSIX C interface to get the relevant data directly.
If you are ok with using Boost libraries, then this should do it http://www.boost.org/doc/libs/1_49_0/doc/html/date_time/examples.html#date_time.examples.seconds_since_epoch
the good part is it also handles time zones conversions :)
You can use system(3) or popen(3), but there's almost always a better, more portable, and native solution, depending on what exactly you're looking for. In your specific case it seems like what you're really looking for a combination of getrusage(2) and ftime(3).
timefunction, and the various functions that formattime_tvariables? E.g. stackoverflow.com/questions/2242963/….