system("ls")

M

Matko

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

Victor Bazarov

Matko said:
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
 
D

Donkey Hottie

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

jl_post

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
 
D

Default User

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
 
G

**Group User**

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
 
A

Abhishek Padmanabh

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
 
R

Ralph Malph

Victor said:
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?
 
A

Andrew Poelstra

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?

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

Ralph Malph

Andrew said:
Then you've clearly been paying infinity times too much
for your software, if it comes with no documentation.
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?
 
A

Andrew Poelstra

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?

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

Alf P. Steinbach

* Andrew Poelstra:
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
 
I

Ian Collins

Ralph said:
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?

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

Matko

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!

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

Klaus ter Fehn

Matko said:
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.
 
J

Jorgen Grahn

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
 
T

tonydee

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Writing "ls" under windows 39
Fix and improve a UDF File System Driver 0
Help with EXT3 Filesystem work 1
C pipe 1
Lexical Analysis on C++ 1
Modifying ls 7
Communicating between processes 0
React+Redux+RTK 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top