Win32_find_data loop to get file size bug

Source: Internet
Author: User
Tags filetime

Today, when debugging the program found a win32_find_data bug, in the loop to read the picture file under a folder, found in the structure of nfilesizelow and nfilesizehigh value = = 0, you can get the file name, But the reading of the file size is 0 embarrassing situation. But in fact, the picture file is normal, size from 80k~170k of all have, compare strange, share with you.

typedef struct _WIN32_FIND_DATAW {
DWORD dwfileattributes;
FILETIME Ftcreationtime;
FILETIME Ftlastaccesstime;
FILETIME Ftlastwritetime;
DWORD Nfilesizehigh;
DWORD Nfilesizelow;
DWORD dwReserved0;
DWORD dwReserved1;
WCHAR cfilename[MAX_PATH];
WCHAR calternatefilename[];
#ifdef _mac
DWORD Dwfiletype;
DWORD Dwcreatortype;
WORD wfinderflags;
#endif
} Win32_find_dataw, *pwin32_find_dataw, *lpwin32_find_dataw;

On-site situation is as follows: Specify a file directory on disk, there are other processes to the directory in the specified format of the JPG file, and the current process will loop to retrieve the directory, the specified format of the JPG file, read and do processing. The code is as follows:

Win32_find_data DATA, cjpg;
HANDLE hfind = NULL;
String C_jpgname;
bool bOK = true;

for (Hfind = FindFirstFile ((Pic_store_path + "*.jpg"). C_str (), &data);
Hfind && bok && fileexists (pic_store_path + data.cfilename); bOK = findnextfile (hfind, &data))
{
C_jpgname = Data.cfilename;

//The problem appears here, I want to retrieve each file size read, less than 5K picture is considered problematic, need to handle, blue part is the code

           if ( Data.nfilesizelow < 5120 && Data.nfilesizehigh = 0)
             {
write_log ("[uploadxml][%d,%d][%s].", Data.nfilesizehigh, Data.nfilesizelow, C_jpgname);                        

write_log ("[Uploadxml] picture exception, stored in the error directory. ");
                        if (fileexists (Pic_error_path + dat) A.cfilename)
                        {
    &N Bsp                           DeleteFile (pic_error_path + dat) A.cfilename);
                       }
                       //Picture size exception, store to error folder
  &NB Sp                     MoveFile ((Pic_store_path + data.cfilename). C_str (), (Pic_error_path + data.cfilename). C_STR ());
                continue;
             }


Actual file-handling code

}

if (hfind)
{
FindClose (hfind);
} else
{
Sleep (100);
}


However, in the test results found that the size of the 80k~178k size of the picture will be placed in the error folder, the print size of [0,0], in the error directory to view these files and nothing unusual, when the images are placed in the correct directory to continue execution, it will not print size 0.


The change will be made to the case of size 0, exception handling.

if (Data.nfilesizelow < 5120 && Data.nfilesizehigh = = 0)
{
Write_log ("[uploadxml][%d,%d][%s].", Data.nfilesizehigh, Data.nfilesizelow, c_jpgname);


if (Data.nfilesizelow = = 0)
{
Sleep (30);
}
Else
                {
Write_log ("[Uploadxml] Picture exception, deposit the error directory. ");
if (fileexists (Pic_error_path + data.cfilename))
                        {
DeleteFile (Pic_error_path + data.cfilename);
                        }
//Picture size exception, store to error folder
MoveFile ((Pic_store_path + data.cfilename). C_str (), (Pic_error_path + data.cfilename). C_STR ()) ;
                }
continue;
             }     

After this modification, the normal pictures will not be placed in the error directory.

But the reason is not very clear, feel this is a bug, paste out and share with you, there are different views can also be explored.





Win32_find_data loop to get file size bug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.