Like Tree2Likes
  • 1 Post By tabstop
  • 1 Post By adeyblue

it doesn't work!

This is a discussion on it doesn't work! within the Windows Programming forums, part of the Platform Specific Boards category; Code: #include <iostream> #include <string> #include <Windows.h> void printFile( HANDLE, WIN32_FIND_DATA ); int main() { HANDLE indice; WIN32_FIND_DATA infoFile; indice ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    it doesn't work!

    Code:
    #include <iostream>
    #include <string>
    #include <Windows.h>
    
    void printFile( HANDLE, WIN32_FIND_DATA );
    
    int main()
    {
    	HANDLE indice;
    	WIN32_FIND_DATA infoFile;
    	
    	indice = FindFirstFile( "*.*", &infoFile );
    
    	printFile( indice, infoFile );
    
    	system( "pause" );
    	return 0;
    }
    
    void printFile( HANDLE index, WIN32_FIND_DATA fileInfo )
    {
    	std::string directory;
    
    	// condizione per poter uscire dal ciclo
    	if ( index == INVALID_HANDLE_VALUE )
    		return;
    	
    	else
    	{
    		std::cout << fileInfo.cFileName << std::endl;
    		
    		// se  una directory 
    		if ( GetFileAttributes( fileInfo.cFileName ) == FILE_ATTRIBUTE_DIRECTORY )
    		{
    			directory = fileInfo.cFileName;
    			directory += "\\*.*";
    			printFile( FindFirstFile( directory.c_str(), &fileInfo ), fileInfo );
    
    		}
    
    		// vado al prossimo file 
    		if ( FindNextFile( index, &fileInfo ) )
    			printFile( index, fileInfo );
    	}
    }
    Why my source doesn't work?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What are you expecting it to do?
    Which parts are giving you trouble?
    What is it doing wrong?

    hint: "Why my source doesn't work?" is immesurably vague and not likely to evince useable answers. If you want explicit answers, ask explicit questions...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    There's a tree walker in the FAQ.
    Perhaps start there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    OK sorry. When I start it there is a runtime error. It could show on the screen all file, directory and files that there are into the directory. If I start it I'll see a lot of points!
    Code:
    .
    .
    .
    .
    .
    Ecc.
    This is all.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    P.S. I think that the problem is in to the second if block

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not sure if this is your issue, but I believe GetFileAttributes may give you a bunch of attributes or'ed together, so you probably want to check && rather than ==. (EDIT: Obviously I mean & and not &&.)

    What is more likely your issue is that . is the first file returned, and . is a directory, so you are then recursively visiting the same directory over and over and over and over and over and over and over and over and over and over and over again.
    Last edited by tabstop; 07-26-2011 at 08:37 AM.
    Salem likes this.

  7. #7
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Quote Originally Posted by tabstop View Post
    Not sure if this is your issue, but I believe GetFileAttributes may give you a bunch of attributes or'ed together, so you probably want to check && rather than ==.
    You'd think they'd remember after being told exactly that last time, but I guess not.

    There's a tree walker in the FAQ.
    Perhaps start there.
    OP, do this.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Francesco Leone View Post
    OK sorry. When I start it there is a runtime error. It could show on the screen all file, directory and files that there are into the directory. If I start it I'll see a lot of points!
    Code:
    .
    .
    .
    .
    .
    Ecc.
    This is all.
    The dot means "current folder" or "this folder"... it is repeating because you are not advancing the pointer. I noticed that you have your call to FindFirstFile() and FindNextFile() in different functions... it seems likely the handle is not propogating across your function boundaries properly. Try rewriting them as a tight loop...

    Code:
    WIN32_FIND_DATA fileinfo;
    HANDLE  hDir;
    
    hDir = FindFirstFile("*.*",&fileinfo);
    if (hDir == INVALID_HANDLE_VALUE)
      { printf("Well that certainly didn't work!");
         exit(1); }
    do
      {  
        // process your file data here 
      }
    while ( FindNextFile(hDir, &fileInfo) != ERROR_NO_MORE_FILES);
    
    FindClose(hDir);
    Also look up the WIN32_FIND_DATA struct on MSDN... you should not need GetFileAttributes or any other function as the struct has it all.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    There's a tree walker in the FAQ.
    Perhaps start there.
    Directory recursion is very easy...

    Code:
    //This is the only Directory Structure Used
    WIN32_FIND_DATA DirData;
    
    //Temporary buffer for path names
    char FPath[MAX_PATH];
    
    
    // recursive directory search
    void WalkDirs(PCHAR SPath)
      { HANDLE HDir;
        // switch to specified direcory
        if (!SetCurrentDirectory(SPath))
          { printf("Error Invalid Pathname\n");
            exit(GetLastError()); }
    
        // Show Full Pathname
        GetCurrentDirectory(MAX_PATH,FPath);
        printf("--> %s\n",FPath);
    
        // begin directory scan
        HDir = FindFirstFile("*.*",&DirData);
        do
          { if (DirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
              { // ignore . and .. sillyness
                if ((strcmp(DirData.cFileName,".") != 0) &&
                     (strcmp(DirData.cFileName,"..") != 0))
                  { // call myself to test a new directory
                    WalkDirs(DirData.cFileName);
                    // done, now back up one level
                    SetCurrentDirectory("..");
                    // Show Pathname
                    GetCurrentDirectory(MAX_PATH,FPath);
                    printf("<-- %s\n",FPath); } }
            else
              // list filenames
              printf("\t\t%s\n",DirData.cFileName);
          
        while (FindNextFile(HDir,&DirData) != ERROR_NO_MORE_FILES);
        FindClose(HDir); }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Sorry CommonTater, but your use of braces is just awful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Sorry CommonTater, but your use of braces is just awful.
    I know. But it just doesn't look right to me any other way... It's a habit that started way back in Turbo Basic (1981) and has stuck with me ever since. I know I'm a lousy example for code formatting but it is only text formatting... the code works.

    POIDE (Pelles IDE) has syntax highlighting and brace matching tools that actually (I think) make it real easy to keep track of braces... I do tend to take advantage of that with bright red braces and medium gray comments so my code really stands out amongst them... I dunno... It works for me

    Here's what it looks like on my editor screen...

    Name:  codesnip.PNG
Views: 159
Size:  15.4 KB
    Last edited by CommonTater; 07-26-2011 at 11:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. Why doesn't it work???
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2006, 09:24 PM
  3. Why Doesn't This Work??!!
    By adinclik in forum C Programming
    Replies: 4
    Last Post: 04-17-2006, 06:20 AM
  4. Why doesn't it work?
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2005, 03:40 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 03:53 PM

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