Unix Programming - problems compiling with and readdir

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2006 > problems compiling with and readdir





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author problems compiling with and readdir
shyam

2006-11-21, 7:24 am

I have a simple directory reading function

DIR *dir;
struct dirent *dirp;


dir = opendir( /* some valid path */ );
while ( (dirp = readdir( dir )) != NULL )
printf( "%s\n", dirp->d_name );


When I compile it in linux 2.4 with gcc 3.2 I get following error

for line conatining the statement
dir = opendir( /* some valid path */ );
the gcc says "warning: assignment from incompatible pointer type"

and for line
printf( "%s\n", dirp->d_name );
it says " dereferencing pointer to imcomplete type"

i tried with including dirent.h as well as sys/dir.h but no lick

it is such a simple thing and atill not working ,
any help will be appreciated

thanks

loic-dev@gmx.net

2006-11-21, 7:24 am

Hello,

> I have a simple directory reading function
>
> DIR *dir;
> struct dirent *dirp;
>
>
> dir = opendir( /* some valid path */ );
> while ( (dirp = readdir( dir )) != NULL )
> printf( "%s\n", dirp->d_name );
>
>
> When I compile it in linux 2.4 with gcc 3.2 I get following error
>
> for line conatining the statement
> dir = opendir( /* some valid path */ );
> the gcc says "warning: assignment from incompatible pointer type"
>
> and for line
> printf( "%s\n", dirp->d_name );
> it says " dereferencing pointer to imcomplete type"
>
> i tried with including dirent.h as well as sys/dir.h but no lick
>
> it is such a simple thing and atill not working ,
> any help will be appreciated


Do you include the two following headers?
#include <sys/types.h>
#include <dirent.h>

You can find out the header(s) to include on your system for a
particular API using the man pages:
$ man opendir

Or, alternatively, for portable programs, you can read the Single Unix
Specification:
http://www.unix.org/version3/online.html

Cheers,
Loic.

shyam

2006-11-21, 7:24 am


loic-dev@gmx.net wrote:
> Hello,
>
>
> Do you include the two following headers?
> #include <sys/types.h>
> #include <dirent.h>


yes , I have included both the headers
also tried with sys/dir.h

but in vein

>
> You can find out the header(s) to include on your system for a
> particular API using the man pages:
> $ man opendir
>
> Or, alternatively, for portable programs, you can read the Single Unix
> Specification:
> http://www.unix.org/version3/online.html
>
> Cheers,
> Loic.


Darko

2006-11-21, 7:24 am

Did you try looking at your man pages? Look for "man opendir", see
which headers you should include, and what types it needs.

shyam wrote:[vbcol=seagreen]
> loic-dev@gmx.net wrote:
>
> yes , I have included both the headers
> also tried with sys/dir.h
>
> but in vein
>

Bjorn Reese

2006-11-22, 7:27 am

shyam wrote:

> When I compile it in linux 2.4 with gcc 3.2 I get following error
>
> for line conatining the statement
> dir = opendir( /* some valid path */ );
> the gcc says "warning: assignment from incompatible pointer type"


The following worked just fine on Linux 2.4.22 with gcc 3.2.2.

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

int main(void)
{
DIR *dir;
struct dirent *dirp;

dir = opendir(".");
if (dir) {

while ( (dirp = readdir( dir )) != NULL )
printf( "%s\n", dirp->d_name );

closedir(dir);
}

return 0;
}


--
mail1dotstofanetdotdk
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com