File read/write details

Source: Internet
Author: User

Check whether the directory exists:

 

Bool Folderexist (cstring strpath)
{
Win32_find_data WFD;
Bool Rvalue = false;
Handle hfind = findfirstfile (strpath, & WFD );
If (hfind! = Invalid_handle_value )&&
(WFD. dwfileattributes & file_attribute_directory ))
{
Rvalue = true;
}
Findclose (hfind );
Return rvalue;
}

 

File existence check:
Note: This function is used to check whether the file exists in the current directory.
If you want to check whether the file exists in other directories, enter the complete path of the file in the parameter.

 

Bool Fileexist (cstring strfilename)
{
Cfilefind ffind;
Return ffind. findfile (strfilename );
}

 

Create directory:

Bool Createfolder (cstring strpath)
{
Security_attributes Attrib;
Attrib. binherithandle = false;
Attrib. lpsecuritydescriptor = NULL;
Attrib. nlength = sizeof (security_attributes );
//The property defined above can be omitted.
//Direct useReturn: createdirectory (path, null );You can.
Return: createdirectory (strpath, & attrib );
}

 

 

File Size:
DWORD getfilesize (cstring filepath)
{
Win32_find_data fileinfo;
Handle hfind;
DWORD filesize;
Cstring filename;
Filename = filepath;
Hfind = findfirstfile (filename, & fileinfo );
If (hfind! = Invalid_handle_value)
Filesize = fileinfo. nfilesizelow;

Findclose (hfind );
Return filesize;
}

Of course, there is the getlength () function in cfilefind, which can also be obtained.

Folder size
DWORD cvctestdlg: getdirsize (cstring strdirpath)
{
Cstring strfilepath;
DWORD dwdirsize = 0;

Strfilepath + = strdirpath;
Strfilepath + = "//*.*";

Cfilefind finder;
Bool bfind = finder. findfile (strfilepath );
While (bfind)
{
Bfind = finder. findnextfile ();
If (! Finder. isdots ())
{
Cstring strtemppath = finder. getfilepath ();
If (! Finder. isdirectory ())
{
Dwdirsize + = finder. getlength ();
}
Else
{
Dwdirsize + = getdirsize (strtemppath );
}
}
}
Finder. Close ();
Return dwdirsize;
}


{
Tchar szdir [max_path] = {null };
Getcurrentdirectory (sizeof (szdir)/sizeof (tchar), szdir );
Lptstr lptfilename = "tstestfunction.exe ";
Lptstr lptpath = szdir;
Lptstr * lptfilepath = & lptfilename;
DWORD dflag = getfullpathname (lptfilename, sizeof (szdir)/sizeof
(Tchar), lptpath, lptfilepath );
// After the preceding statement is executed, lptfilename, lptpath, and lptfilepath will get the correct result.
}

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.