NAME_MAX

Discussion in 'C Programming' started by occhiverdi, Jul 9, 2004.

  1. occhiverdi

    occhiverdi Guest

    Hi,

    I would like to know which file do I need to include to get NAME_MAX? I'm
    trying to read filenames of a directory but it gave me error saying that
    the NAME_MAX is undefined. I've looked into the /usr/include directory and
    I see that it's not defined anywhere.

    #include <dirent.h>
    ....
    struct dirent * file;
    char filename[NAME_MAX + 1];
    ....
    while ((file = readdir(queue_dir)) != NULL) {
    filename = file -> d_name;
    ....

    Thanks for help.

    occhiverdi
     
    occhiverdi, Jul 9, 2004
    #1
    1. Advertising

  2. occhiverdi

    Ben Pfaff Guest

    occhiverdi <> writes:

    > I would like to know which file do I need to include to get NAME_MAX?


    Standard C doesn't have a NAME_MAX. There is a FILENAME_MAX in
    <stdio.h>.
    --
    "When I have to rely on inadequacy, I prefer it to be my own."
    --Richard Heathfield
     
    Ben Pfaff, Jul 9, 2004
    #2
    1. Advertising

  3. occhiverdi

    Quentarez Guest

    I believe you must #include <limits.h>.


    "occhiverdi" <> wrote in message
    news:IwzHc.12860$...
    > Hi,
    >
    > I would like to know which file do I need to include to get NAME_MAX? I'm
    > trying to read filenames of a directory but it gave me error saying that
    > the NAME_MAX is undefined. I've looked into the /usr/include directory and
    > I see that it's not defined anywhere.
    >
    > #include <dirent.h>
    > ...
    > struct dirent * file;
    > char filename[NAME_MAX + 1];
    > ...
    > while ((file = readdir(queue_dir)) != NULL) {
    > filename = file -> d_name;
    > ...
    >
    > Thanks for help.
    >
    > occhiverdi
     
    Quentarez, Jul 9, 2004
    #3
  4. occhiverdi

    osmium Guest

    occhiverdi writes:

    > I would like to know which file do I need to include to get NAME_MAX? I'm
    > trying to read filenames of a directory but it gave me error saying that
    > the NAME_MAX is undefined. I've looked into the /usr/include directory and
    > I see that it's not defined anywhere.
    >
    > #include <dirent.h>
    > ...
    > struct dirent * file;
    > char filename[NAME_MAX + 1];
    > ...
    > while ((file = readdir(queue_dir)) != NULL) {
    > filename = file -> d_name;
    > ...


    Perhaps you want FILENAME_MAX? I would expect it to be in <stdio.h>.
     
    osmium, Jul 9, 2004
    #4
  5. occhiverdi

    Ben Pfaff Guest

    "Quentarez" <> writes:

    > I believe you must #include <limits.h>.


    <limits.h> does not contain NAME_MAX.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Jul 9, 2004
    #5
  6. occhiverdi

    occhiverdi Guest

    occhiverdi wrote:

    > #include <dirent.h>
    > ...
    > struct dirent * file;
    > char filename[NAME_MAX + 1];
    > ...
    > while ((file = readdir(queue_dir)) != NULL) {
    > filename = file -> d_name;
    > ...



    Thanks for the replies.

    I see that I only have to declare filename as
    char * filename;
    It works.

    occhiverdi
     
    occhiverdi, Jul 9, 2004
    #6
  7. occhiverdi wrote:

    > Hi,
    >
    > I would like to know which file do I need to include to get NAME_MAX?


    Include <stdio.h> to get FILENAME_MAX. There is no standard C macro
    named 'NAME_MAX'.
     
    Martin Ambuhl, Jul 9, 2004
    #7
  8. Emmanuel Delahaye, Jul 9, 2004
    #8
  9. occhiverdi

    -berlin.de Guest

    Emmanuel Delahaye <> wrote:
    > In 'comp.lang.c', occhiverdi <> wrote:


    >> I see that I only have to declare filename as
    >> char * filename;
    >> It works.


    > What works? I'm scared...


    In that case it might even work, because he's just copying the
    pointer to some member of a structure the function returns,
    he's not calling e.g. strcpy() with that pointer.
    <OT>
    Of course, that only works as expected if the OP doesn't want to
    keep what the pointer is pointing to after the next invocation of
    the readdir() function, i.e. when he uses 'filename' only as an
    abbreviation for 'file->d_name'.
    </OT>
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 9, 2004
    #9
  10. Emmanuel Delahaye <> writes:
    > In 'comp.lang.c', occhiverdi <> wrote:
    > > I see that I only have to declare filename as
    > > char * filename;
    > > It works.

    >
    > What works? I'm scared...


    When I first read occhiverdi's response, I was also concerned that he
    was incorrectly using an uninitialized char*. But if you'll read
    the full article (including the part that you snipped), you'll see
    that he wrote:

    > filename = file -> d_name;


    which is perfectly legitimate assuming that file->d_name is valid.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 9, 2004
    #10
  11. In 'comp.lang.c', Keith Thompson <> wrote:

    > Emmanuel Delahaye <> writes:
    >> In 'comp.lang.c', occhiverdi <> wrote:
    >> > I see that I only have to declare filename as
    >> > char * filename;
    >> > It works.

    >>
    >> What works? I'm scared...

    >
    > When I first read occhiverdi's response, I was also concerned that he
    > was incorrectly using an uninitialized char*. But if you'll read
    > the full article (including the part that you snipped), you'll see
    > that he wrote:
    >
    >> filename = file -> d_name;

    >
    > which is perfectly legitimate assuming that file->d_name is valid.


    Agreed. I have read too fast. My bad.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 9, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page