• Advertisement

Archived

This topic is now archived and is closed to further replies.

Running a command in stdlib.h

This topic is 5063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m running some commands in my c++ program (with System(cmd). The command works fine, but how do i capture the output?? If i run System("dir"), how can i store the output in variables ??

Share this post


Link to post
Share on other sites
Advertisement
First off, using system() is not a good practice. See this thread for details on why.

Secondly, there''s no portable/reliable method to capture output from the process spawned by calling system(). If the command shell supports redirection of standard output you may be able to do something like:

int main(int, char **) {
system("dir > outfile.txt");
std::ifstream ifs("outfile.txt");
std::copy(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char*gt;(std::cout));

return 0;
}

Share this post


Link to post
Share on other sites
You can't. Or, rather, it's difficult and non-intuitive to try to do it this way. system() starts a new process and executes the command. Output is directed to the standard output streams (stdout, stderr) as character strings. If you want to capture a directory listing, it won't be through system() calls. You could redirect stdout and try to parse the text stream, but that's making far more work for yourself than you really need to.

There are functions defined in dirent.h that can facilitate handling of directories. DIR *pdir=opendir (const char *path) will open up a directory. You can then use pdir to iterate through the directory's contents. int closedir(DIR * pdir) will close the directory, after which you can no longer use pdir . Finally, dirent* pfile readdir(DIR * pdir) will read the next directory entry, or return NULL when there are no more entries. A loop on readdir() can be used to iterate through all directory entries until NULL is returned. You can use int rewinddir(DIR *pdir) to rewind a directory listing to the start of the directory.

The dirent structure, I believe, will usually be platform-specific, meaning that the actual structure members may vary from filesystem to filesystem.

Note that dirent.h is POSIX, so I don't know if Visual Studio provides it. Cygwin/Mingw provide it, though. For Visual Studio, you may need to rely on Windows-specific directory handling, and I can't help you there.


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller


[edited by - VertexNormal on April 12, 2004 5:28:26 PM]

Share this post


Link to post
Share on other sites
For the record, dirent.h does not seem to be recognized by Visual C++. The equivalent functionality is available through FindFirstFile() and its support functions in Win32. However, a better idea is to use boost::filesystem which will give you (mostly) portable file system functionality.

Share this post


Link to post
Share on other sites
I suggest you use cstdlib instead of stdlib.h.

[edited by - Drevay on April 12, 2004 7:22:47 PM]

Share this post


Link to post
Share on other sites

  • Advertisement