Obtain the file size in Linux

Source: Internet
Author: User

From: http:// OS .51cto.com/art/200912/168700.html

We often encounter problems and difficulties in our study and life. For example, when we are learning Linux, for example, how to obtain the file size in Linux. A few days ago, I needed to write a piece of code to get the size of some video files. I thought: Isn't it easy? Simply use the standard C file to operate the function. So I wrote the following code to implement it:

 
 
  1. unsigned long get_file_size(const char *filename)  
  2. {  
  3.     unsigned long size;  
  4.     FILE* fp = fopen( filename, "rb" );  
  5.     if(fp==NULL)  
  6.     {  
  7.         printf("ERROR: Open file %s failed.\n", filename);  
  8.         return 0;  
  9.     }  
  10.     fseek( fp, SEEK_SET, SEEK_END );  
  11.     size=ftell(fp);  
  12.     fclose(fp);  
  13.     return size;  

Unexpectedly, some files can be correctly retrieved after execution, while some files cannot be correctly retrieved and the code is checked, there is nothing wrong with it. However, a problem is found in this process, that is, the files with the correct size are relatively small, and the files with errors are large. So I thought it would be because the standard C file operation function does not support files larger than a certain size. So I googled and did not expect that the guess was correct, file operation functions of Standard C do not support reading files larger than 2 GB.

After finding the problem, it seems that there is only one way to implement it, because some functions of Standard C are rarely used at ordinary times, so we only need to turn to Google, after reading a lot of reference articles on the internet, I found that calling the stat function can correctly obtain the status information of a large file (including the file size), so I finally implemented the following code:

 
 
  1. unsigned long get_file_size(const char *filename)  
  2. {  
  3.     struct stat buf;  
  4.     if(stat(filename, &buf)<0)  
  5.     {  
  6.         return 0;  
  7.     }  
  8.     return (unsigned long)buf.st_size;  

From writing such a small function, we can see that it is really important to accumulate more computer knowledge at ordinary times. At the same time, it is also very important to fully test the code. Otherwise, looking at the correct code may cause unexpected troubles in some cases. In this way, you can learn how to get the file size in Linux.

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.