By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

reading filenames in C

P: n/a
Tim
Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?

I did this, but it seems goofy:

sprintf(t, "ls *.csv > filenames.txt");
system(t);
fptr = fopen("filenames.txt", "r");
while (!(feof(fptr)))
{
fgets(line, 100, fptr);
blah-blah-blah

Is there a way to do this without creating another file?

-Tim
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tim wrote:

Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?

I did this, but it seems goofy:

sprintf(t, "ls *.csv > filenames.txt");
system(t);
fptr = fopen("filenames.txt", "r");
while (!(feof(fptr)))
{
fgets(line, 100, fptr);
blah-blah-blah

Is there a way to do this without creating another file?


Not in Standard C, which does not assume a file system
that has a notion of "directory."

<off-topic>"man readdir", and take any further questions
to comp.unix.programmer.</off-topic>

--
Er*********@sun.com
Nov 14 '05 #2

P: n/a
In article <e9**************************@posting.google.com >,
ya*****@hotmail.com says...
Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?
You would be better off asking in one of the Solaris development
groups. This group is about standard C only.
I did this, but it seems goofy:


Yes. I can pretty much guarantee there are better ways to do
it than that on your target platform.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 14 '05 #3

P: n/a

"Tim" <ya*****@hotmail.com> wrote:
e9**************************@posting.google.com...
Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?

I did this, but it seems goofy:
...


You can try POSIX functions opendir(), readdir() and closedir():

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

int main(void)
{
DIR* pdir = opendir("/");
struct dirent* pent;
if(pdir)
{
while(pent = readdir(pdir))
{
printf("entry: %s\n", pent->d_name);
}
closedir(pdir);
}
return 0;
}

See the manual page for more information.

Best Regards,
Julian
Nov 14 '05 #4

P: n/a
On Wed, 17 Dec 2003 10:23:49 +0800, "Julian Zhang"
<ju******@263.sina.com> wrote in comp.lang.c:

"Tim" <ya*****@hotmail.com> wrote:
e9**************************@posting.google.com...
Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?

I did this, but it seems goofy:
...


You can try POSIX functions opendir(), readdir() and closedir():


Please don't vandalize comp.lang.c with off-topic answers. POSIX is
not topical here. It is in news:comp.unix.programmer and many other
groups.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #5

P: n/a
Tim
As far as C is concerned, how "portable" is POSIX? Would I run into
any trouble if I needed to compile this code in a couple of different
UNIX environments?

<I wasn't sure if this question was better suited to the UNIX
group...>

-Tim

Jack Klein <ja*******@spamcop.net> wrote in message news:<2s********************************@4ax.com>. ..
On Wed, 17 Dec 2003 10:23:49 +0800, "Julian Zhang"
<ju******@263.sina.com> wrote in comp.lang.c:

"Tim" <ya*****@hotmail.com> wrote:
e9**************************@posting.google.com...
Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?

I did this, but it seems goofy:
...


You can try POSIX functions opendir(), readdir() and closedir():


Please don't vandalize comp.lang.c with off-topic answers. POSIX is
not topical here. It is in news:comp.unix.programmer and many other
groups.

Nov 14 '05 #6

P: n/a
Tim
Ignore my question about "portability," after I turned my brain on and
did a quick search over at webopedia.com I realized that POSIX is
designed to make code portable.

Thanks for all the help!
-Tim

Jack Klein <ja*******@spamcop.net> wrote in message news:<2s********************************@4ax.com>. ..
On Wed, 17 Dec 2003 10:23:49 +0800, "Julian Zhang"
<ju******@263.sina.com> wrote in comp.lang.c:

"Tim" <ya*****@hotmail.com> wrote:
e9**************************@posting.google.com...
Does anyone know a way to read the filenames from a given directory in
C in a Solaris environment?

I did this, but it seems goofy:
...


You can try POSIX functions opendir(), readdir() and closedir():


Please don't vandalize comp.lang.c with off-topic answers. POSIX is
not topical here. It is in news:comp.unix.programmer and many other
groups.

Nov 14 '05 #7

P: n/a
Tim writes:
Ignore my question about "portability," after I turned my brain on and
did a quick search over at webopedia.com I realized that POSIX is
designed to make code portable.

Thanks for all the help!
-Tim

Jack Klein <ja*******@spamcop.net> wrote in message

news:<2s********************************@4ax.com>. ..
On Wed, 17 Dec 2003 10:23:49 +0800, "Julian Zhang"
<ju******@263.sina.com> wrote in comp.lang.c:

"Tim" <ya*****@hotmail.com> wrote:
e9**************************@posting.google.com...
> Does anyone know a way to read the filenames from a given directory in > C in a Solaris environment?
>
> I did this, but it seems goofy:
> ...

You can try POSIX functions opendir(), readdir() and closedir():


Please don't vandalize comp.lang.c with off-topic answers. POSIX is
not topical here. It is in news:comp.unix.programmer and many other
groups.


If you look real close at the thread, you will see that Jack Klein, who you
thank, actually chastised someone else for trying to help you. You are a
true gentleman. Or something.
Nov 14 '05 #8

P: n/a
"osmium" <r1********@comcast.net> writes:
[...]
If you look real close at the thread, you will see that Jack Klein, who you
thank, actually chastised someone else for trying to help you. You are a
true gentleman. Or something.


As we've discussed at length here, redirecting off-topic questions to
a more appropriate newsgroup is actually more helpful than trying to
answer them here. If I want to post something about POSIX, I'd much
rather post it over in comp.unix.programmer, where they know enough
about it to correct my inevitable errors.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.