C + + Get file size

Source: Internet
Author: User
Tags function prototype

During the interview, the interviewer mentions how to get the file size, which is summarized as follows:

Test the size of the file in the C language, using two standard functions primarily.

1.fseek

Function prototype: int fseek (FILE * stream, long int offset, int origin);

Parameter description: Stream, file stream pointer, offest, offset, Orgin, original (start position). The optional values for Orgin are Seek_set (file start), Seek_cur (the current position of the file pointer), Seek_end (end of file).

Function Description: For a stream opened in binary mode, the new stream location is Origin + offset.

2.ftell

Function prototype: Long int ftell (FILE * stream);

Function Description: Returns the position of the stream. For binary streams, the return value is the number of bytes from the starting position of the file.

Get file size C program:

    1. int file_size (char* filename)
    2. {
    3. FILE *fp=fopen (filename,"R");
    4. if (!FP) return-1;
    5. fseek (Fp,0l,seek_end);
    6. int size=ftell(FP);
    7. Fclose (FP);
    8. return size;
    9. }

The above method uses Fseek to move the access location of a file to the end of the file, and then uses Ftell to obtain the current file access location. This method can be considered as an indirect way of acquiring. Although the file size can be obtained, there are two disadvantages. First, the return value of Ftell is long, and the number of bytes occupied in different environments is different, which may be a long is four bytes. At this point, the obtained file size cannot exceed 2G, otherwise an error occurs.

However, the above disadvantages are not problematic in most cases, and large files can also get file sizes through Fsetpos and fgetpos. The most deadly flaw is that it needs to load the file into memory and then jump to the end of the file, which is time consuming! may not be able to read a small number of files, but when the file reaches tens of thousands of times, the speed will be very slow, this method is equivalent to all the files read into memory again!

If possible, try to avoid using the above indirect way to get the file size. Under Linux, there is also a much simpler way to get file size by reading file information, which is much faster.

    1. #include <sys/stat.h>
    2. int file_size2 (char* filename)
    3. {
    4. struct stat statbuf;
    5. Stat (FILENAME,&STATBUF);
    6. int size=statbuf.st_size;
    7. return size;
    8. }

This method first obtains the state information of the relevant file, and then reads the size information from the state information. The operation is very fast because there is no operation to read the file. It is strongly recommended that you use this method under Linux. The Windows platform must also have a similar function to read file information, but I do not program in Windows all year round, so do not introduce.

In addition, you can use the STATFS function to view disk space:

#include <sys/statfs.h>#include<stdio.h>intMain () {structStatfs DiskInfo; Statfs ("/home/carl/", &diskinfo); unsignedLong LongBlockSize = diskinfo.f_bsize;//the number of bytes contained in each blockUnsignedLong LongTotalSize = blocksize * diskinfo.f_blocks;//total number of bytes, f_blocks to blockprintf"total_size =%llu B =%llu KB =%llu MB =%llu gb\n", TotalSize, TotalSize>>Ten, totalsize>> -, totalsize>> -); unsignedLong LongFreedisk = Diskinfo.f_bfree * BLOCKSIZE;//the size of the remaining spaceUnsignedLong LongAvailabledisk = Diskinfo.f_bavail * BLOCKSIZE;//Free space Sizeprintf"Disk_free =%llu mb =%llu gb\ndisk_available =%llu mb =%llu gb\n", Freedisk>> -, freedisk>> -, availabledisk>> -, availabledisk>> -); return 0; }  

C + + Get file size

Related Article

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.