FindFirstFile

Im trying to list files and folders. This is my code but cant get the path to work.my path is C:\\users\\myname\\desktop\\

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>
#include <direct.h>
#include <windows.h>

using namespace std;
char data[12800];
char buf2[256];
int main(int argc, char *argv[])
{
    char chFolderpath[12800];

strcpy(chFolderpath, ("C:\\users\\myname\\desktop\\"));
  HANDLE hFind;
WIN32_FIND_DATA data2;
strcat(data,"");
hFind = FindFirstFile(chFolderpath, &data2);

strcat(data,"\r\n");
if (hFind != INVALID_HANDLE_VALUE) {
	while (FindNextFile(hFind, &data2)){
if ( data2.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
strcat(data,"<DIR>");

   
		strcat(data,data2.cFileName);
        strcat(data,"\r\n");
    
}}
cout << data;
system("PAUSE");
    return EXIT_SUCCESS;
}

I'd like to know more information. You want to access a file, or directory?
both

I also want to enter a directory...with cin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <direct.h>
#include <windows.h>

using namespace std;
char data[12800];
char buf2[256];
int main(int argc, char *argv[])
{
char buf2[256];

    cout << "ENTER A DIRECTORY\n";
cin >> buf2;


  HANDLE hFind;
WIN32_FIND_DATA data2;
strcat(data,"");
hFind = FindFirstFile(buf2, &data2);

strcat(data,"\r\n");
if (hFind != INVALID_HANDLE_VALUE) {
	while (FindNextFile(hFind, &data2)){
if ( data2.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
strcat(data,"<DIR>");

   
		strcat(data,data2.cFileName);
        strcat(data,"\r\n");
    
}}
cout << data;
system("PAUSE");
    return EXIT_SUCCESS;
}




still does not work
Last edited on
Your indentation is absolutely terrible. It makes it very difficult to follow the program flow.

Here it is cleaned up, with edits/corrections and comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdlib>
#include <iostream>
//#include <direct.h>  <- don't know what this header is, but you don't need it
#include <string>  // <- use strings
#include <windows.h>

using namespace std;
//char data[12800];  // get rid of this junk, use strings
//char buf2[256];
int main(int argc, char *argv[])
{
    //  NOTE:  you need to put a '*' wildcard character in the search string.  This was your biggest problem:
    static const char* chFolderpath = "C:\\users\\myname\\desktop\\*";  // no need to copy this into a buffer, just use a const
    string data;  // you new data buffer

    HANDLE hFind;
    WIN32_FIND_DATAA data2; // <- WIN32_FIND_DATAA if using char strings (instead of TCHAR strings)

    // strcat(data,""); <- this was killing you before, as 'data' was uninitialized.  You probably meant strcpy, not strcat
    //  but regardless, get rid of the char buffers because strings are easier/safer

    hFind = FindFirstFileA(chFolderpath, &data2);  // <- FindFirstFileA if using char strings (instead of TCHAR strings)

    // strcat(data,"\r\n");  <- no need for this
    if (hFind != INVALID_HANDLE_VALUE) 
    {
        // at this point, 'data2' actually contains the first entry, so you don't want to call FindNextFile until
        //  you record it.  A do/while loop is more appropriate than a while loop for this reason:
        do
        {
            if( data2.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
                data += "<DIR>";  // strings use += instead of strcat

            data += data2.cFileName;
            data += '\n'; // you really only need '\n'.  \r\n is just for outputting to windows controls, really
        } while( FindNextFileA( hFind, &data2 ) );  // FindNextFileA if using char strings (instead of TCHAR strings)

        FindClose( hFind );  // don't forget to close it when you're done!
    }
    cout << data;
    system("PAUSE");
    return EXIT_SUCCESS;
}
nice, it works

thanx
Last edited on
Topic archived. No new replies allowed.