Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

listing files probelm with WIN32_FIND_DATA

hi im making a program thats searching through a directory for files for their filenames and i keep getting an error message that cFileName is not declared. here is my code( other parts not done yet)

void searchFiles(string dir, string newDir)
{
string error;
string finddir = dir + "*.txt";
LPWIN32_FIND_DATA first, file;
HANDLE firstfile = FindFirstFile(finddir.c_str(), first);

DWORD dw = GetLastError();
getError(dw, error);

if(error != "INVALID_HANDLE_VALUE")
{

string filename = first.cFileName;
FindNextFile(firstfile, file);
}

}


any help would be great

Comments

  • StaxozeroStaxozero Member Posts: 3
    : hi im making a program thats searching through a directory for files
    : for their filenames and i keep getting an error message that
    : cFileName is not declared. here is my code( other parts not done yet)
    :
    : void searchFiles(string dir, string newDir)
    : {
    : string error;
    : string finddir = dir + "*.txt";
    : LPWIN32_FIND_DATA first, file;
    : HANDLE firstfile = FindFirstFile(finddir.c_str(), first);
    :
    : DWORD dw = GetLastError();
    : getError(dw, error);
    :
    : if(error != "INVALID_HANDLE_VALUE")
    : {
    :
    : string filename = first.cFileName;
    : FindNextFile(firstfile, file);
    : }
    :
    : }
    :
    :
    : any help would be great
    :


    Never mind everything has been worked out
  • stoberstober Member Posts: 9,765 ✭✭✭
    :
    : void searchFiles(string dir, string newDir)
    : {
    : string error;
    : string finddir = dir + "*.txt";
    : LPWIN32_FIND_DATA first, file;
    : HANDLE firstfile = FindFirstFile(finddir.c_str(), first);
    :
    : DWORD dw = GetLastError();
    : getError(dw, error);
    :
    : if(error != "INVALID_HANDLE_VALUE")
    : {
    :
    : string filename = first.cFileName;
    : FindNextFile(firstfile, file);
    : }
    :
    : }
    :
    :
    : any help would be great
    :


    That's not the correct way to do error handling for this. You don't have to call GetLastError() or getError(). Just test the return value of FindFirstFile() for INVALID_HANDLE_VALUE, like this

    [code]
    HANDLE firstfile = FindFirstFile(finddir.c_str(), first);
    if( firstfile != INVALID_HANDLE_VALUE)
    {
    string filename = first.cFileName;
    do
    {
    if( stramp(first.cFileName,".") != 0 && stramp(first.cFileName,"..") != 0 )
    {
    // valid file name

    }

    } while FindNextFile(firstfile, file);
    }
    [/code]
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]I made a correction:[/color]

    : [code]:
    : HANDLE firstfile = FindFirstFile(finddir.c_str(), first);
    : if( firstfile != INVALID_HANDLE_VALUE)
    : {
    : string filename = first.cFileName;
    : do
    : {
    : if( stramp(first.cFileName,".") != 0 && stramp(first.cFileName,"..") != 0 )
    : {
    : // valid file name
    :
    : }
    :
    : } while FindNextFile(firstfile, file);
    : [color=Red]FindClose (firstfile);[/color]
    : }
    : [/code]:

Sign In or Register to comment.