Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   system("ls") (http://www.velocityreviews.com/forums/t712541-system-ls.html)

Matko 01-21-2010 08:07 PM

system("ls")
 
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 01-21-2010 08:22 PM

Re: system("ls")
 
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

Donkey Hottie 01-21-2010 08:56 PM

Re: system("ls")
 
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?

jl_post@hotmail.com 01-21-2010 09:00 PM

Re: system("ls")
 
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

Default User 01-21-2010 09:29 PM

Re: system("ls")
 
jl_post@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

**Group User** 01-22-2010 04:55 AM

Re: system("ls")
 
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:
>
> > 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

Abhishek Padmanabh 01-22-2010 07:02 AM

Re: system("ls")
 
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.../simple_ls.cpp

Ralph Malph 01-22-2010 04:22 PM

Re: system("ls")
 
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 01-22-2010 04:23 PM

Re: system("ls")
 
On 2010-01-22, Ralph Malph <ralph.malph@altavista.com> 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.


Ralph Malph 01-22-2010 04:27 PM

Re: system("ls")
 
Andrew Poelstra wrote:
> On 2010-01-22, Ralph Malph <ralph.malph@altavista.com> 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?


All times are GMT. The time now is 08:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.