C/C ++ multiple methods to get the file size

Source: Internet
Author: User

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!

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.