Thread: using the same file handle

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    using the same file handle

    hi!
    below, i enumerate through a folder, and when i get the 'no more files' error i try to use the same file handle again to find files from the beggining, after calling FindClose(). but i immediately get the 'no more files' error again. can't i use a same file handle for subsequent file finds? or is there's something i'm missing?
    Code:
    	srand( (unsigned)time( NULL ));
    	int begin = rand();
    
    	strcat(fpath,"\\*.*");
    FNDAGN:
    	int faerr=0;
    	fh = FindFirstFile(fpath, &finfo);
    	if(fh == INVALID_HANDLE_VALUE)
    	{
    		ErrorMSG("blah blah blah");
    		return 0;
    	}else{
    		for(int i=0; i<begin; i++)
    		{
    			FindNextFile(fh, &finfo);
    			if(GetLastError()==18)
    			{
    				FindClose(fh);
    				fh = NULL;
    				goto FNDAGN;
    			}
    		}

    thanx in advance 4 any help.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Typically, you can only call GetLastError after a function has failed, as determined by its return value. Most functions do not clear the last error value when successful.
    Code:
    if (!FindNextFile(fh, &finfo))
    {
        // Okay, the function has failed, now we can check GetLastError...
    }

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    the finfo struct contains the info on the last file found.
    That was the last file in the folder.
    That is, when FindFirstFile() is called the second time, finfo is not reset, and the find starts at the last file.

    try some thing like (and reinit)
    ZeroMemory(&finfo,sizeof(WIN32_FIND_DATA ));

    I have to say the GOTO is bad style.
    It is not needed here.
    Is a sign of poor design.

    [jk]
    Leave GOTO in VB where it belongs....
    [/jk]
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    thanx.

    That is, when FindFirstFile() is called the second time, finfo is not reset, and the find starts at the last file.
    but it seems to work without calling ZeroMemory(), after i changed the code as pointed by anonytmouse. isn't FindFirstFile() starts to finds file starting at the value inside the file handle?
    Last edited by geek@02; 09-17-2005 at 09:08 PM.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM