FindNextFile() problem

kobi's Avatar, Join Date: Feb 2007
Light Poster
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);


//////need to open the file//////////////

    cout << FindData.cFileName << endl;
// Look for more

    while (FindNextFile(hFind, &FindData))
    {
        cout << FindData.cFileName << endl;
    }

// Close the file handle

    FindClose(hFind);

    return 0;
}
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Use the FindData structure to get the name and path to the file and use the Normal File Open methods like fopen / fstream methods.
0
kobi's Avatar, Join Date: Feb 2007
Light Poster
Quote:
Originally Posted by DaWei
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.
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
0
kobi's Avatar, Join Date: Feb 2007
Light Poster
Quote:
Originally Posted by shabbir
Use the FindData structure to get the name and path to the file and use the Normal File Open methods like fopen / fstream methods.
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
kobi's Avatar, Join Date: Feb 2007
Light Poster
Quote:
Originally Posted by shabbir
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.
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?
0
kobi's Avatar, Join Date: Feb 2007
Light Poster
Quote:
Originally Posted by shabbir
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.
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;
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You dont need to open the Dir but just get into the dir recursively
0
kobi's Avatar, Join Date: Feb 2007
Light Poster
Pls can you explain how to do it

Thanks