A file's length?

A file's length?

How many bytes for a specified file?
 1 
 2  #if (defined __unix__) || (defined _WIN64) || (defined _WIN32)
 3  #include  < sys / types.h >
 4  #include  < sys / stat.h >
 5  #else
 6  #include  < fstream >
 7  #endif
 8 
 9  #if  (defined __unix__)
10 
11  unsigned  long   long  file_length( const   char *  file)
12  {
13     struct  stat64 st;
14     int  err  =  stat64(file,  & st);
15     if ( 0   !=  err)
16       return   0 ;
17     return  static_cast < unsigned  long   long > (st.st_size);
18  }
19 
20  #elif  (defined _WIN32) || (defined _WIN64)
21 
22  //  Microsoft has some extensions to support Unix!
23  unsigned  long   long  file_length( const   char *  file)
24  {
25     struct  __stat64 st;
26     int  err  =  _stat64(file,  & st);
27     if ( 0   !=  err)
28       return   0 ;
29     return  static_cast < unsigned  long   long > (st.st_size);
30  }
31 
32  #else
33 
34 // the last alternative
35  unsigned  long   long  file_length( const   char *  file)
36  {
37    std::ifstream fin(file, std::ios_base:: in );
38     if ( ! fin.is_open())
39       return   0 ;
40    fin.seekg( 0 , std::ios_base::beg);
41    std::ifstream::pos_type pos_start  =  fin.tellg();
42    fin.seekg( 0 , std::ios_base::end);
43    std::ifstream::pos_type pos_end  =  fin.tellg();
44     return  static_cast < unsigned  long   long > (pos_end  -  pos_start);
45  }
46 
47  #endif
48 

你可能感兴趣的:(A file's length?)