FindNextFile() problem

Discussion in 'C++' started by kobi, Feb 14, 2007.

  1. kobi

    kobi New Member

    Joined:
    Feb 14, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hello
    Im trying to open files in a specific directory and analize them,
    i get to the stage of listinf all the files in that directory but i can get to open them,
    please if some one can tell how to open them because i get always an error.


    Code:
    #include <windows.h> 
    #include <iostream> 
    using namespace std;
    
    int main()
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
    
        
    // Find the first file
    
        hFind = FindFirstFile("C:\\ibloga.blogspot.com\\*.*", &FindData);
    
    
    [B]//////need to open the file//////////////[/B]
    
        cout << FindData.cFileName << endl;
    // Look for more
    
        while (FindNextFile(hFind, &FindData))
        {
            cout << FindData.cFileName << endl;
        }
    
    // Close the file handle
    
        FindClose(hFind);
    
        return 0;
    }
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Possibly I don't understand your question. Are you expecting FindFirstFile/FindNextFile to open the files? They don't. Perhaps, though, you are asking how to open a file, or how to understand the information returned by FindFirstFile/FindNextFile. Please clarify.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Use the FindData structure to get the name and path to the file and use the Normal File Open methods like fopen / fstream methods.
     
  4. kobi

    kobi New Member

    Joined:
    Feb 14, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for answearing
    Sorry if i didnt explain well, after getting the first file and the second i need to open them & to read ,write.
    The posted code is working well but i dont succed to open ,the first the second etc...

    Pls if you can show how to open
     
  5. kobi

    kobi New Member

    Joined:
    Feb 14, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for answearing

    im not so familair with FindData structure pls can you show me how to open the first file
    after taking the path.

    i was trying using fopen after concatenating the name of the file and his location,but when trying to open it i get null in the file pointer.

    Thanks
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    FindData refer - http://msdn2.microsoft.com/en-us/library/aa365740.aspx

    In FindData you will get the filename in TCHAR cFileName[MAX_PATH]; and append it to the path and you will get the complete file path and then use that with fopen.

    If that does not work then try giving a hard coded path to your drive and see if it works.
     
  7. kobi

    kobi New Member

    Joined:
    Feb 14, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks - i have succed to open the files ,but how can i open a directory found in a given directory,the idea is : im looking to open all the files and the directoris which i found under a given inicial directory ,and process all the files which i have found?
     
  8. kobi

    kobi New Member

    Joined:
    Feb 14, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Just to add this is my code:

    Code:
    #include "stdafx.h"
    #include <windows.h> 
    #include <iostream> 
    #include <fstream> 
    using namespace std;
    
    int main()
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        FILE *fp;
    	char filename[_MAX_PATH];
    	char c;
    	int x=0;
    
    
       printf("testing \n");
    
      
    // Find the first file
    
        hFind = FindFirstFile("C:\\test\\*.*", &FindData);
    	
        	printf(FindData.cFileName);
    	printf("\n");
    	  
    	// Look for more
      
    while (FindNextFile(hFind, &FindData))
        {
            if( strcmp(FindData.cFileName,".") != 0 && strcmp(FindData.cFileName,"..") != 0)
    	  
    			{
    				
                    printf(FindData.cFileName);
    				strcpy(filename,"c:\\test\\");
    				strcat(filename, FindData.cFileName);
    				fp = fopen(filename, "rt");
    				if(fp==NULL)
    					printf("problem on open the file");
    				x=0;
    				c=fgetc(fp);/*Reading of file character by charactercommences here*/
    				
    				while (c!=EOF)
    					{
    						 //Process file						 c=fgetc(fp);
    						 
    		
    					}
    								
    				fclose(fp);
    			}
    		
    	 }
    
    // Close the file handle
    
        FindClose(hFind);
    	  
    
    	  return 0;
    }
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You dont need to open the Dir but just get into the dir recursively
     
  10. kobi

    kobi New Member

    Joined:
    Feb 14, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Pls can you explain how to do it

    Thanks
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    For directories inside the C:\test\ folders you should be calling the same routine of FindFirstFile and FindNextFile and loop through them.

    Something like
    Code:
    // Get first file name 
    hFind = ::FindFirstFile( szPath, &FindData );
    
    do
    {
    	// Skip current folder ".", parent folder ".." for infinite recursion through directories
    	if( FindFileData.cFileName == _TEXT(".") || FindFileData.cFileName == _TEXT("..") )
    		continue;
    
    	// Recurcuve search if filenamle is a folder
    	if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
    	{
    		// Recurse through the new directory in same fashion
    	}
    
    } while( ::FindNextFile(hFind, &FindFileData) != 0 );
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice