How to determine whether a file exists (VC)

Source: Internet
Author: User

Most recently, file operations are performed in projects. the most commonly used method is to open a file, read and write the file, and determine whether the file exists. this article does not discuss file read/write, but only checks whether a file exists.

Remember that in C #, there is something called cfile. Through its method cfile. Exit (filename), you can directly determine whether the file exists, but this method is not available in VC.

At first, I defined a file variable and opened the file to be judged. If 0 is returned, the file does not exist. this method requires two steps. The first step is to define a variable of the file class. The second step is to use this variable to directly open a file and check its return value. If it is 0, it indicates that it does not exist. this method is still relatively simple, but it will waste some resources. In addition, if this file exists, close the file after judgment. If it is not closed, problems may occur. in VC, there are several file classes. You can choose which one you like. the code used is as follows:

Cstdiofile filetemp2;
If (filetemp2.open (filename, cfile: moderead) = 0 ))
{// If this file does not exist
MessageBox (_ T ("the file you selected does not exist. Please reselect it! "));
}

Later, we found another method, getfileattributes, which can be used to determine whether a file exists without defining variables. The method is as follows:

If (getfileattributes ("C: // test.bmp") =-1)
{
MessageBox (null, _ T ("the file does not exist! "), _ T (" system error "), mb_iconerror );
}

This method is very useful and is recommended.

Of course, I have not tried other methods, because there is only one good thing. However, if the above method cannot meet your requirements, you can try the following methods (I have not tested it, find it online ):

<1>:  CFileFind find;BOOL IsFinded = find.FindFile("C://Test.bmp");
If (isfinded) {// exists}
Else {// does not exist}
Use the findfile () and findnextfile functions of the cfilefind class for search.
C: // not found, that is, the file does not exist. For specific function usage, see msdn
<2>:  1、BOOL PathFileExists(LPCTSTR lpszPath); SHELL API
2、DWORD GetFileAttributes(LPCTSTR lpFileName);   API
<3>: 1.CFileFind f;  
(This is the first method above)
if (f.FindFile(g_szCalFileName,0))
{// Do your processing}
else{    
MessageBox ("file not exsit! "," Prompt ", mb_ OK + mb_iconinformation );
return;}
2. If you want to open a file, you can:
CFile file;
if(!file.Open(filepath,CFile::modeRead))
MessageBox("error");
else{
file.Close();
return;}

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.