- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Accessing the directory listing into a string ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 07:00 AM
тАО03-11-2002 07:00 AM
Accessing the directory listing into a string directly
I want to list the files and directories in a directory using the system call
system("ls
I can redirect the output to a file. But again I need to read it into a string.
Can some one please help me if there is any way that I can access the output into a string directly without using file.
Please reply me for any queries.
Thanks in advance
Umesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 07:26 AM
тАО03-11-2002 07:26 AM
Re: Accessing the directory listing into a string directly
you could use command substitution:
var=`ls /directory_to_list`
or
var=$(ls /directory_to_list)
if you use posix- or korn- shell. The first syntax works also for bourne- shells.
Allways stay on the bright side of life!
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 07:33 AM
тАО03-11-2002 07:33 AM
Re: Accessing the directory listing into a string directly
First of all, it would be extremely dangerous to attempt to load an entire directory into a string. I suppose what you mean is how to avoid the temporary file. Use the popen() function.
char s_cmd[256],s_tmp[512],*p = NULL;
char *my_dir = "/tmp/";
FILE *f = NULL;
int cc = 0;
extern int errno;
(void) sprintf(s_cmd,"ls %s",my_dir);
f = popen(s_cmd,"r");
if (f != NULL)
{
s[0] = '\0';
p = fgets(s_tmp,(int) sizeof(s_tmp),f);
while (p != NULL)
{
printf("%s",s_tmp);
p = fgets(s_tmp,(int) sizeof(s_tmp),f);
}
cc = pclose(f);
}
else cc = (errno != 0) ? errno : -1;
That should do it for you, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 07:38 AM
тАО03-11-2002 07:38 AM
Re: Accessing the directory listing into a string directly
Ooops...
Change
s[0] = '\0';
to
s_tmp[0] = '\0';
Actually, you could leave the statement out; it's just that I tend to initialize everything. The other area of concern is to make sure that the s_cmd variable is big enough to hold your largest directory name and that s_tmp is sized big enough to hold the largest filename plus a Linefeed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 08:04 AM
тАО03-11-2002 08:04 AM
Re: Accessing the directory listing into a string directly
SHAME on you for initializing variables!
Isn't it funny how a lot of today's programmers leave so much to "chance"? Like when they HARDCODE array length's, thinking that no one will ever need more than X rows. Or they decide to abuse malloc to get memory, without really thinking about other elegant solutions to the problem. Or the compiler will initialize it for me....
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 08:07 AM
тАО03-11-2002 08:07 AM
Re: Accessing the directory listing into a string directly
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 09:59 AM
тАО03-11-2002 09:59 AM
Re: Accessing the directory listing into a string directly
Regards,
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-11-2002 11:16 AM
тАО03-11-2002 11:16 AM
Re: Accessing the directory listing into a string directly
See man, there are some example.