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; }
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.
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 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
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
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?
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; }
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 );