Thanks!
God bless you!
Consider reading the manual about programming your OS. In Windows, for
example, you examine the contents of the directory by means of the
FindFirstFile/FindNextFile API calls. I heard that Boost had some kind
of semi-portable directory access classes/interfaces, check them out too.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dear Matko,
I know of no standard C++-specific way of doing this, but I do know
you can use the C functions opendir() and readdir() to do this.
Read the documentation for those functions for details, but in a
nutshell, opendir() returns a DIR*, which can then be passed into
readdir() multiple times, each time returning a dirent*. This dirent
object has a d_name member which points to the filename (essentially
the char* you're looking for).
Here's a short sample program to get you started:
#include <dirent.h> // for opendir(), readdir(), and closedir()
#include <iostream>
int main(int argc, char ** argv)
{
DIR *dir = opendir("."); // open the current directory
if (!dir)
{
std::cerr << "Cannot open directory!" << std::endl;
exit(1);
}
struct dirent *entry;
while (entry = readdir(dir)) // notice the single '='
{
std::cout << "Found directory entry: "
<< entry->d_name << std::endl;
}
closedir(dir);
return 0;
}
After creating the DIR*, remember to check to make sure it is non-
NULL before using it with readdir(). Do not free (or delete) the DIR
and dirent handles, but remember to close the DIR handle with closedir
() or it will remain open for the rest of your program.
Eventually readdir(dir) will return NULL, which lets you know it is
done returning dir entries. If you want more details, I urge you to
read those functions' documentation.
Many third-party C++ libraries (like Trolltech's Qt) have cleaner
(and more C++-oriented) ways of getting directory entries. If you
have access to one, I recommend using it. If you can't, opendir() and
readdir() are quite portable and should suit your needs.
> Thanks!
> God bless you!
Thank you. And God bless you, too!
-- Jean-Luc
popen() might be what you ask, but what Victor Bazarov wrote might what
you need.
--
Are you ever going to do the dishes? Or will you change your major to
biology?
> On Jan 21, 1:07 pm, Matko <mkl...@foi.hr> wrote:
> >
> > Is there a better way to get data returned by 'ls' command instead
> > of 'system("ls > file.txt");' and then reading the file. I'm
> > thinking of something that would return 'const char *' into my
> > buffer.
> I know of no standard C++-specific way of doing this, but I do know
> you can use the C functions opendir() and readdir() to do this.
No you don't, because there are no such C functions. There are POSIX
functions of those names. The fact that the OP is asking about ls makes
it a good chance those would work.
Brian
> Hi!
> Is there a better way to get data returned by 'ls' command instead of
> 'system("ls > file.txt");' and then reading the file. I'm thinking of
> something that would return 'const char *' into my buffer.
If you have ls, there is a good chance you have glob as well, maybe this
suits your needs better. See: man 3 glob.
hth
Paavo
What a nice resource for my family Hahahhhehe. We will and definitely
can turn you on and off whenever we wish to
Hehehh
Yes, its boost::filesystem lib. They also have an example that does
try to replicate 'ls' which should be useful:
http://www.boost.org/doc/libs/1_41_0/libs/filesystem/example/simple_ls.cpp
Then you've clearly been paying infinity times too much
for your software, if it comes with no documentation.
I think you should have installed Linux, which is free and
comes with hundreds of manpages, plus a few free compilers
and development tools. :)
(Hardware, sadly, is not so cheap.)
Huh. It's not like there is a lack of free tools and docs for Windows and OS X.
It's just not printed on flakes of dead trees.
For iterating over directories the OP could do well to check out the file system
functionality in the Boost library.
The Boost library is, by the way, free. :-)
Cheers & hth.,
- Alf
Have you seen that new fangled internet thingy? Any decent OS will have
heaps of vendor documentation on line. Get hold of a web browser and
have a look.
--
Ian Collins
http://pstreams.sourceforge.net is a more flexible alternative to
popen using iostreams.
ipstream in("ls");
std::string s;
while (in >> s)
...
What you want is this:
http://www.boost.org/doc/libs/1_41_0/libs/filesystem/doc/index.htm
--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
Thank You very much everyone for your effort!
I am pleased with your responds. Nevertheless, i have used system("ls >
file.txt") which was sufficient for my case.
God bless you!
What happens if there was a file.txt already in the directory where
you are running the program from (possibly a file you don't want to
destroy)?
What happens if you run the program in a directory where you don't
have write permissions to?
If you insist on using 'ls' then using a pipe would be best. Otherwise
try get the informations you need by using system calls or other library
functions into your program - that would be much faster.
--
Klaus ter Fehn
... to boldly code where no byte has gone before ...
Maybe not so much *faster*, but safer. 'ls' formats the listing for
printing, but file names with odd characters (like '\n') will break,
if he intends to parse and *use* the listing.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Bit harsh! It's fair to call them C functions, as while they're not
be part of the C Standard, they are written in C, and that in itself
is significant as a way of saying you won't get any nice C++-specific
usage conventions....
Back to the main topic - opendir()/readdir() are a good solid
"professional" solution. For the record, the most direct realisation
of Matko (the OP)'s system("ls") is through popen(), which allows the
results of the real "ls" to be read and parsed....
Cheers,
Tony