directories, lccwin32 and findnextfile

This is a discussion on directories, lccwin32 and findnextfile within the Windows Programming forums, part of the Platform Specific Boards category; i've been trying to write a piece of code to use findfirstfile, findnextfile to list the contents of a directory, ...

  1. #1
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435

    directories, lccwin32 and findnextfile

    i've been trying to write a piece of code to use findfirstfile, findnextfile to list the contents of a directory, and of course i've failed miserably. if somebody has worked with these functions before and is willing to help, i'll paste my broken code. but what would really be great is a piece of plain c code that uses these functions to traverse directories (writing a hackish wrapper to do posix functions like opendir).. if you have something like that lying around, it'd be great. oh yes, i'm using windows me if that makes a difference.

    tia
    .sect signature

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I do not have any example code, but if you post your code I willl take a look and try to help.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    ok, here is the code (forgive me):

    Code:
    /* give sort of posix function support for directory manipulation.... in win32~!!! */
    #ifndef DIRENT_C
    #define DIRENT_C
    
    /* OH NOS I HAET YOU WINDOWS YOU @#^$ERES YUO DO NOT LET TEH DIRECTORIUES L:IST : ^_^ */
    
    #include "dirent.h"
    
    /*
    struct dirent {
    	char d_name[260];
    };
    
    typedef struct {
    	unsigned long _d_hdir;
    	char *_d_dirname;
    	unsigned _d_magic;
    	unsigned _d_nfiles;
    	char _d_buf[sizeof(WIN32_FIND_DATA)];
    } DIR;
    
    typedef struct _WIN32_FIND_DATA { // wfd
        DWORD dwFileAttributes;
        FILETIME ftCreationTime;
        FILETIME ftLastAccessTime;
        FILETIME ftLastWriteTime;
        DWORD    nFileSizeHigh;
        DWORD    nFileSizeLow;
        DWORD    dwReserved0;
        DWORD    dwReserved1;
        TCHAR    cFileName[ MAX_PATH ];
        TCHAR    cAlternateFileName[ 14 ];
    } WIN32_FIND_DATA;
    */
    
    /* open up a directory.... POSIX STYLE! LOR~ (BUT NOT COMPLIANT OH NOS) */
    DIR *opendir(const char *dirname)
    {
    	DIR *t; WIN32_FIND_DATA *d;
    
    	char feh[4096];
    
    	t = (DIR *) malloc(sizeof(DIR));
    	if(t == NULL) return t;
    	d = (WIN32_FIND_DATA *) &t->_d_buf;
    
    	/* try to grab directory handle */
    	t->harhar = FindFirstFile(dirname, d);
    	if(t->harhar == INVALID_HANDLE_VALUE)
    		{ free(t); return NULL; };
    
    	/* is this even a directory? (is that air you're breathing? h4w h4w) */
    	if(!(d->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    		{ free(t); return NULL; };
    
    	/* copy in some basic information */
    	t->_d_dirname = (char *) malloc(sizeof(char) * strlen(dirname) + 1);
    	if(t->_d_dirname == NULL) { free(t); return NULL; };
    	strcpy(t->_d_dirname, dirname);
    	memcpy(&t->_d_hdir, &t->harhar, sizeof(unsigned long)); /* stall0wned, beey0tch compiler!!1 */
    
    	/* prep up the clue train!!!1 */
    	strcpy(d->cFileName, t->_d_dirname);
    	strcat(d->cFileName, "/*");
    
    	/* count all of the files! ONE TWO THREE */
    	t->_d_nfiles = 0;
    	while(FindNextFile(t->harhar, d) != 0)
    	{
    		strcpy(d->cFileName, t->_d_dirname);
    		strcat(d->cFileName, "/*");
    		++t->_d_nfiles;
    	};
    
    	#include <stdio.h>
    	printf("haha! the number of files are %d\n", t->_d_nfiles);
    	printf("lookie here!@: %s\n", d->cFileName);
    
    	/* victory! */
    	return t;
    };
    
    struct dirent *readdir(DIR *dir)
    {
    	struct dirent *dent;
    
    
    	return dent;
    };
    
    /* close our directory handle */
    int closedir(DIR *dir)
    {
    	if(FindClose(dir->harhar) == -1)
    		return -1;
    	free(dir->_d_dirname); free(dir);
    	return 0;
    };
    
    void rewinddir(DIR *dir)
    {
    
    
    	return;
    };
    
    #endif
    the struct definitions from the header file are in the comments on top..

    the problem, besides my stupidity, is that findnextfile doesn't seem to work as documented. i took it that calling it with the same search handle but replacing the name field with a wildcard "*" would do the trick of just grabbing the next file entry from the same directory, but it didn't work. i tried some other things along the same line, but no results....
    .sect signature

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Instead of creating a pointer to a WIN32_FIND_DATA struct try creating that and pass its address. Try this:

    Code:
    WIN32_FIND_DATA d;
    t->harhar = FindFirstFile(dirname, &d);
    This will assure you have the memory for it instead of dynamically allocating that memory. I would try simplifying it by just finding the files and displaying there names in a messagebox, once you get that to work then add the other lines.

    Try using *.* instead of * for the wildcard. This might help.

    Sorry, if that did not help, I do not have much experience with these functions. You could try doing a board search to see if there is any working code someone posted.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i tried different specifier strings like *.*, but again..

    allocating memory wasn't a problem

    i'll try a board search, but this question is still open.
    .sect signature

  6. #6
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    hoo boy, i got it to work. most of the posts for the search results of "directory" just were quick use-google-to-look-for-findfirstfile-and-findnextfile-you-cretin, but one of them finally held the truth: instead of using just the directory in a findfirstfile call, i'm supposed to use the directory with "/*" appended. gee, all that wasted effort. @$(@!# damn the winapi reference and its lack of examplage.
    .sect signature

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    You should also be checking (FindNextFile()==0) for

    GetLastError() == ERROR_NO_MORE_FILES

    to ensure that you ran out of files, not had a different error.

    (and then SetLastError(ERROR_SUCCESS)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21