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;
}
|