reading filenames in C

T

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
 
E

Eric Sosman

Tim said:
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>
 
R

Randy Howard

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.
 
J

Julian Zhang

Tim said:
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
 
T

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
 
T

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
 
O

osmium

Tim said:
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



Please don't vandalize comp.lang.c with off-topic answers. POSIX is
not topical here. It is in and many other
groups.
[/QUOTE]

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.
 
K

Keith Thompson

osmium said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top