Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > system("ls")

Reply
Thread Tools

system("ls")

 
 
Matko
Guest
Posts: n/a
 
      01-21-2010
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!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-21-2010
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
 
Reply With Quote
 
 
 
 
Donkey Hottie
Guest
Posts: n/a
 
      01-21-2010
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?
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      01-21-2010
On Jan 21, 1:07 pm, Matko <(E-Mail Removed)> 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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-21-2010
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Jan 21, 1:07 pm, Matko <(E-Mail Removed)> 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
 
Reply With Quote
 
**Group User**
Guest
Posts: n/a
 
      01-22-2010
On Jan 22, 5:35*am, Paavo Helde <(E-Mail Removed)> wrote:
> Matko <(E-Mail Removed)> wrote innews:hjac57$6da$(E-Mail Removed):
>
> > 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
 
Reply With Quote
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      01-22-2010
On Jan 22, 4:22*am, Victor Bazarov <(E-Mail Removed)> 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.../simple_ls.cpp
 
Reply With Quote
 
Ralph Malph
Guest
Posts: n/a
 
      01-22-2010
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?
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      01-22-2010
On 2010-01-22, Ralph Malph <(E-Mail Removed)> wrote:
> 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?


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

 
Reply With Quote
 
Ralph Malph
Guest
Posts: n/a
 
      01-22-2010
Andrew Poelstra wrote:
> On 2010-01-22, Ralph Malph <(E-Mail Removed)> wrote:
>> 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?

>
> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments