Running a command in stdlib.h

Started by
3 comments, last by Ormurin 20 years ago
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 ??
MAn..
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;} 
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]
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.
I suggest you use cstdlib instead of stdlib.h.

[edited by - Drevay on April 12, 2004 7:22:47 PM]
_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.

This topic is closed to new replies.

Advertisement