opendir doesn't work..

M

Markus Pitha

Hello,
i'm using Gentoo Linux and like to open directories which i got from a
text file i read of. I tried to open a directory as i wrote the
directory name directly into "opendir()" and it worked. When i use this
pointer on the array "input", which contains the actual directory, the
program cannot read the directory.
I already tested if "input" contains the directories at all, but it does.
if ((fp = fopen(fpath, "r")) == NULL) {
fprintf(stderr, "Couldn't open config file");
exit(1);
}
while (!feof (fp) ) {
fgets(input, pathlength, fp);
if ((dir=opendir(input)) != NULL) { /* opendir cannot open the directory, stored in "input" */
while ((dirzeiger=readdir(dir)) != NULL)
printf("%s\n", (*dirzeiger).d_name);
}
closedir(dir);
}
fclose(fp);

Any ideas?
 
M

Markus Pitha

Now i hope the code looks fine...

if ((fp = fopen(fpath, "r")) == NULL) {
fprintf(stderr, "Couldn't open config file");
exit(1);
}
while (!feof (fp) ) {
fgets(input, pathlength, fp);
if ((dir=opendir(input)) != NULL) {
while ((dirzeiger=readdir(dir)) != NULL)
printf("%s\n", (*dirzeiger).d_name);
}
closedir(dir);
}
fclose(fp);
 
F

Flash Gordon

Now i hope the code looks fine...

if ((fp = fopen(fpath, "r")) == NULL) {
fprintf(stderr, "Couldn't open config file");
exit(1);
}
while (!feof (fp) ) {
fgets(input, pathlength, fp);
if ((dir=opendir(input)) != NULL) {
while ((dirzeiger=readdir(dir)) != NULL)
printf("%s\n", (*dirzeiger).d_name);
}
closedir(dir);
}
fclose(fp);

The C language as defined by the ISO/ANSI standards does not support
reading directories. You need to ask in either a Linux programming group
(since your previous post said you use Gentoo) or a unix programming
group such as comp.unix.programmer
 
A

Al Bowers

Markus said:
Now i hope the code looks fine...

if ((fp = fopen(fpath, "r")) == NULL) {
fprintf(stderr, "Couldn't open config file");
exit(1);
}
while (!feof (fp) ) {

There is a faq question on this. Read:
http://www.eskimo.com/~scs/C-faq/q12.2.html

Instead of using the function feof simply make
the while loop:
while(NULL != fgets(input, pathlength, fp))
{
if(dir=opendir(input)) ...etc...
....
 
A

Al Bowers

Al said:
There is a faq question on this. Read:
http://www.eskimo.com/~scs/C-faq/q12.2.html

Instead of using the function feof simply make
the while loop:
while(NULL != fgets(input, pathlength, fp))
{
if(dir=opendir(input)) ...etc...
....

I forgot to mention that function fgets will read the
newline char in most situations. For example, if you
have a line in the file with a directory name of
"windows", function fgets will reading into the
input buffer "windows\n". Depending on the definiton
of function, this might fail. I suggest you remove
the newline char before calling function opendir.

#include <stdio.h>
#include <string.h>

char *s;
while(NULL != fgets(input,pathlength,fp))
{
if((s = strchr(input,'\n')) != NULL) *s = '\0';
if(dir = opendir( .....etc........
.........
 
M

Markus Pitha

Hello Al,
i adopted your proposal and I even solved another problem too, thanks.

Markus.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top