c++ - Opening excel files with system( ) on mac? -
i trying c++ program open existing excel spreadsheet (along bunch of applications), keeps returning error file not exist. using following code:
int main(){ system("open ~/path/file"); //--open applications using same command--// } the file there , command works open applications, i'm not sure doing wrong.
thanks in advance!!
very probably, system /bin/sh -which definition used system(3)- not expand ~.
you might try like
char cmd[256]; snprintf(cmd, sizeof(cmd), "open %s/path/file", getenv("home")); if (0 != system(cmd)) { fprintf(stderr, "%s failed\n", cmd); exit(exit_failure); }; since interactive shells expand ~ $home , home environment variable.
(with c++, use std::string operations instead of snprintf)
my snprintf + system trick not @ failproof. if $home contains spaces or bizarre characters ; or ', wont work. , snprintf might fail (e.g. because $home huge).
of course, you'll better test before getenv("home") not null. might use getpwuid(3) getuid(2) if getenv("home") fails returning null.
on linux want xdg-open instead of open.
Comments
Post a Comment