CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2008
    Posts
    18

    c++ - find first file in directory on linux system?

    all the examples i've seen seem to be for windows and are a
    bit different than what i need. also, findfirstfile is not
    in the textbook i bought to use as reference.

    i need to find the first file in a directory so i can perform
    a loop for processing. all of the files in the directory will
    be of the same type and have the same name except that each
    file is numbered. if i can find the first file in the directory,
    i should be able to parse the name and increment the numbers in
    the loop to move through the directory.

    any tips on finding the first file's name?

    thanks,
    BabaG

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: c++ - find first file in directory on linux system?

    [open/close]dir and readdir from glibc should do the trick.

  3. #3
    Join Date
    May 2008
    Posts
    18

    Re: c++ - find first file in directory on linux system?

    thanks plasmator. found this in searches based off your link:
    Code:
    /* print files in current directory in reverse order */
    
    #include <dirent.h>
    
    main(){
    
        struct dirent **namelist;
    
        int n;
    
        n = scandir(".", &namelist, 0, alphasort); 
    
        if (n < 0) 
            perror("scandir"); 
        else { 
    
            while(n--) { 
                printf("%s\n", namelist[n]->d_name); 
                free(namelist[n]); 
            } 
    
            free(namelist); 
        } 
    }
    does almost exactly what i need so i think i can adapt
    it if i can get this working. it produces errors when
    i compile it. i get:

    perror was not declared in this scope
    printf was not declared in this scope
    free was not declared in this scope
    free was not declared in this scope

    how do i properly declare these? or do i simply have
    the wrong commands there for c++?

    thanks,
    BabaG

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: c++ - find first file in directory on linux system?

    Quote Originally Posted by BabaG
    thanks plasmator. found this in searches based off your link:
    Code:
    /* print files in current directory in reverse order */
    
    #include <dirent.h>
    
    main(){
    
        struct dirent **namelist;
    
        int n;
    
        n = scandir(".", &namelist, 0, alphasort); 
    
        if (n < 0) 
            perror("scandir"); 
        else { 
    
            while(n--) { 
                printf("%s\n", namelist[n]->d_name); 
                free(namelist[n]); 
            } 
    
            free(namelist); 
        } 
    }
    does almost exactly what i need so i think i can adapt
    it if i can get this working. it produces errors when
    i compile it. i get:

    perror was not declared in this scope
    printf was not declared in this scope
    free was not declared in this scope
    free was not declared in this scope

    how do i properly declare these? or do i simply have
    the wrong commands there for c++?

    thanks,
    BabaG
    The code you found is for 'C' compilers.

    The code will not compile for C++ for various reasons. First, In C++, all functions must be declared or defined before using them. You just can't call printf() without it being declared. You are missing the <cstdio> header. You are also missing the headers that declare perror() and free().

    Second, the main() function returns an int.
    Code:
    int main()
    All you have is main().

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2008
    Posts
    18

    Re: c++ - find first file in directory on linux system?

    thanks paul.

    added:

    #include <cstdio>
    int main()
    return 0

    and am now left with only:

    free was not declared in this scope
    free was not declared in this scope

    how do i declare 'free'?

    thanks again,
    BabaG

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: c++ - find first file in directory on linux system?

    #include <cstdlib> for free().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2008
    Posts
    18

    Re: c++ - find first file in directory on linux system?

    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured