Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

system("ls")

5 views
Skip to first unread message

Matko

unread,
Jan 21, 2010, 3:07:03 PM1/21/10
to
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.

Thanks!
God bless you!

Victor Bazarov

unread,
Jan 21, 2010, 3:22:42 PM1/21/10
to
Matko 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.

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

jl_...@hotmail.com

unread,
Jan 21, 2010, 4:00:28 PM1/21/10
to
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.


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

Donkey Hottie

unread,
Jan 21, 2010, 3:56:35 PM1/21/10
to
On 21.1.2010 22:07, Matko wrote:
> 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.
>

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?

Default User

unread,
Jan 21, 2010, 4:29:02 PM1/21/10
to
jl_...@hotmail.com wrote:

> 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

Paavo Helde

unread,
Jan 21, 2010, 5:35:32 PM1/21/10
to
Matko <mkl...@foi.hr> wrote in news:hjac57$6da$1...@news1.carnet.hr:

> 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

**Group User**

unread,
Jan 21, 2010, 11:55:20 PM1/21/10
to
On Jan 22, 5:35 am, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> Matko <mkl...@foi.hr> wrote innews:hjac57$6da$1...@news1.carnet.hr:

What a nice resource for my family Hahahhhehe. We will and definitely
can turn you on and off whenever we wish to
Hehehh

Abhishek Padmanabh

unread,
Jan 22, 2010, 2:02:34 AM1/22/10
to
On Jan 22, 4:22 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Matko 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.
>
> 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.

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

Ralph Malph

unread,
Jan 22, 2010, 11:22:16 AM1/22/10
to
Victor Bazarov wrote:
> Matko 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.
>
> Consider reading the manual about programming your OS.
What year is this? 1981?
Computers don't come with those sorts of manuals
anymore. Vendors make way too much money selling
documentation and training. Why should they
just give it away?

Andrew Poelstra

unread,
Jan 22, 2010, 11:23:39 AM1/22/10
to

Then you've clearly been paying infinity times too much
for your software, if it comes with no documentation.

Ralph Malph

unread,
Jan 22, 2010, 11:27:44 AM1/22/10
to
I think you meant "hardware" not "software".
Anyway, I just bought a Windows laptop the other
day. Sadly, it did not come with a system
programming guide like you think it should.
What discount should I have asked for?

Andrew Poelstra

unread,
Jan 22, 2010, 11:49:42 AM1/22/10
to

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.)


Alf P. Steinbach

unread,
Jan 22, 2010, 11:56:03 AM1/22/10
to
* Andrew Poelstra:

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

Ian Collins

unread,
Jan 22, 2010, 2:17:54 PM1/22/10
to

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

use...@kayari.org

unread,
Jan 23, 2010, 4:12:56 PM1/23/10
to
On Jan 21, 8:56 pm, Donkey Hottie <don...@fred.pp.fi> wrote:
> On 21.1.2010 22:07, Matko wrote:
>
> > 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.
>
> popen() might be what you ask, but what Victor Bazarov wrote might what
> you need.

http://pstreams.sourceforge.net is a more flexible alternative to
popen using iostreams.

ipstream in("ls");
std::string s;
while (in >> s)
...

Juha Nieminen

unread,
Jan 24, 2010, 12:40:10 PM1/24/10
to
Matko 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.

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 ---

Matko

unread,
Jan 29, 2010, 7:48:40 AM1/29/10
to

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!

Juha Nieminen

unread,
Jan 29, 2010, 2:35:53 PM1/29/10
to
Matko wrote:
> I am pleased with your responds. Nevertheless, i have used system("ls >
> file.txt") which was sufficient for my case.

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?

Klaus ter Fehn

unread,
Jan 30, 2010, 6:27:33 AM1/30/10
to
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.

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 ...

Jorgen Grahn

unread,
Feb 3, 2010, 6:49:19 AM2/3/10
to
On Sat, 2010-01-30, Klaus ter Fehn wrote:
> 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.
>
> 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.

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 .

tonydee

unread,
Feb 3, 2010, 9:08:58 PM2/3/10
to
On Jan 22, 6:29 am, "Default User" <defaultuse...@yahoo.com> wrote:

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

0 new messages