Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   FindNextFile() problem (http://www.go4expert.com/forums/findnextfile-problem-t3014/)

kobi 15Feb2007 00:39

FindNextFile() problem
 
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;
}


DaWei 15Feb2007 05:13

Re: FindNextFile() problem
 
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.

shabbir 15Feb2007 09:04

Re: FindNextFile() problem
 
Use the FindData structure to get the name and path to the file and use the Normal File Open methods like fopen / fstream methods.

kobi 15Feb2007 12:32

Re: FindNextFile() problem
 
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

kobi 15Feb2007 12:39

Re: FindNextFile() problem
 
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

shabbir 15Feb2007 12:53

Re: FindNextFile() problem
 
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.

kobi 16Feb2007 14:10

Re: FindNextFile() problem
 
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?

kobi 16Feb2007 14:14

Re: FindNextFile() problem
 
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;
}


shabbir 16Feb2007 22:20

Re: FindNextFile() problem
 
You dont need to open the Dir but just get into the dir recursively

kobi 16Feb2007 22:33

Re: FindNextFile() problem
 
Pls can you explain how to do it

Thanks


All times are GMT +5.5. The time now is 00:27.