system("ls")

Discussion in 'C++' started by Matko, Jan 21, 2010.

  1. Matko

    Matko Guest

    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!
    Matko, Jan 21, 2010
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 21, 2010
    #2
    1. Advertising

  3. 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?
    Donkey Hottie, Jan 21, 2010
    #3
  4. Matko

    Guest

    On Jan 21, 1:07 pm, 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.



    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
    , Jan 21, 2010
    #4
  5. Matko

    Default User Guest

    wrote:

    > On Jan 21, 1:07 pm, 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.


    > 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
    Default User, Jan 21, 2010
    #5
  6. On Jan 22, 5:35 am, Paavo Helde <> wrote:
    > Matko <> wrote innews:hjac57$6da$:
    >
    > > 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
    **Group User**, Jan 22, 2010
    #6
  7. On Jan 22, 4:22 am, 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.  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
    Abhishek Padmanabh, Jan 22, 2010
    #7
  8. Matko

    Ralph Malph Guest

    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?
    Ralph Malph, Jan 22, 2010
    #8
  9. On 2010-01-22, Ralph Malph <> 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.
    Andrew Poelstra, Jan 22, 2010
    #9
  10. Matko

    Ralph Malph Guest

    Andrew Poelstra wrote:
    > On 2010-01-22, Ralph Malph <> 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?
    Ralph Malph, Jan 22, 2010
    #10
  11. On 2010-01-22, Ralph Malph <> wrote:
    > Andrew Poelstra wrote:
    >> On 2010-01-22, Ralph Malph <> 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?


    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.)
    Andrew Poelstra, Jan 22, 2010
    #11
  12. * Andrew Poelstra:
    > On 2010-01-22, Ralph Malph <> wrote:
    >> Andrew Poelstra wrote:
    >>> On 2010-01-22, Ralph Malph <> 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?

    >
    > 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
    Alf P. Steinbach, Jan 22, 2010
    #12
  13. Matko

    Ian Collins Guest

    Ralph Malph 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?


    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
    Ian Collins, Jan 22, 2010
    #13
  14. Matko

    Guest

    On Jan 21, 8:56 pm, Donkey Hottie <> 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)
    ...
    , Jan 23, 2010
    #14
  15. Matko

    Matko Guest

    On Thu, 21 Jan 2010 20:07:03 +0000, 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.
    >
    > 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!
    Matko, Jan 29, 2010
    #15
  16. 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.


    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 ...
    Klaus ter Fehn, Jan 30, 2010
    #16
  17. Matko

    Jorgen Grahn Guest

    On Sat, 2010-01-30, Klaus ter Fehn 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.

    >
    > 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 .
    Jorgen Grahn, Feb 3, 2010
    #17
  18. Matko

    tonydee Guest

    On Jan 22, 6:29 am, "Default User" <> wrote:
    > wrote:
    > > On Jan 21, 1:07 pm, Matko <> wrote:
    > >    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


    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
    tonydee, Feb 4, 2010
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sfunds sfunds
    Replies:
    9
    Views:
    8,712
  2. Charles A. Lackman

    Streaming with System.IO and System.Net

    Charles A. Lackman, Dec 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    545
    Richard Grimes [MVP]
    Jan 15, 2005
  3. Mark Miller
    Replies:
    1
    Views:
    1,074
    Mark Miller
    Jul 18, 2003
  4. Matt
    Replies:
    2
    Views:
    834
    Matt Getting
    Jul 24, 2003
  5. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    713
    Kelsang Wangchuk
    Jul 31, 2003
Loading...

Share This Page