Download source code: Click to download
The source code is as follows:
# Include < Iostream >
# Include < Io. h >
# Include < Sys \ Stat. h >
# Include < Afx. h >
# Define _ Afxdll
Using Namespace STD;
void main ()
{< br> /// this file is inaccessible when the project is open
char * filepath = " .. \ test. NCB " ;
//Method 1
Struct_ Stat Info;
_ Stat (filepath,&Info );
IntSize=Info. st_size;
Cout<Size<Endl;
// Method 2
File * File = Fopen (filepath, " RB " );
If (File)
{
Int Size = Filelength (fileno (File ));
Cout < Size < Endl;
Fclose (File );
}
//Method 3
Cfile;
If(Cfile. Open (filepath, cfile: moderead ))
{
IntSize=Cfile. getlength ();
Cout<Size<Endl;
}
// Method 4
Handle handle = Createfile (filepath, file_read_ea, file_pai_read, 0 , Open_existing, 0 , 0 );
If (Handle ! = Invalid_handle_value)
{
Int Size = Getfilesize (handle, null );
Cout < Size < Endl;
Closehandle (handle );
}
}
Vs2005: If the compilation link fails, modify the Project Settings:
(1) configuration properties-> C/C ++-> code generation-> Runtime Library, select "multi-threaded debug (/MTD )"
(2) configuration properties-> linker-> input-> ignore specific library, input "msvcprtd. lib"
In this example, the test. NCB file cannot be opened when the project is open, so method 2 and method 3 cannot get the file size. Therefore, we recommend that you use method 1 and method 4.
If you have other methods, please add them.
Thank you for adding the fourth method to where the thief is located!