How to get the file size in Linux

Source: Internet
Author: User
1. Shell
// Obtain the used disk space in bytes.
# Ifdef _ Linux
Sprintf (CMD, "cd % s; find. -Name /"*. */"| xargs LS-L | awk '{size + = $5} end {print size }'",
Str_filepath.c_str ());
// Sprintf (CMD, "Du-S % S | awk '{print $1}'", str_filepath.c_str ());
# Endif

If (FP = popen (CMD, "R "))! = NULL)
{
Fgets (CMD, sizeof (CMD), FP );
}
Else
{
Log (null, 0, "shell error: cd % s; find. -Name /"*. */"| xargs LS-L | awk '{size + = $5} end {print size }'");
Cout <"shell error: cd % s; find. -Name /"*. */"| xargs LS-L | awk '{size + = $5} end {print size}'" <Endl;
}

2.

Unsigned long get_file_length (File * file_ptr)
{
Unsigned long poscur = 0;
Unsigned long posbegin = 0;
Unsigned long posend = 0;

If (null = file_ptr)
{
Return 0;
}

Poscur = ftell (file_ptr); // obtain the current position of the file pointer
Fseek (file_ptr, 0l, seek_set); // move the file pointer to the start of the file
Posbegin = ftell (file_ptr); // obtain the location of the current file, that is, the location of the file header.
Fseek (file_ptr, 0l, seek_end); // move the file pointer to the end of the file
Posend = ftell (file_ptr); // obtain the end position of the file.
Fseek (file_ptr, poscur, seek_set); // move the pointer to the original position of the file

Return posend-posbegin; // returns the file size.
}

3.
Long
Get_file_size (char * filename)
{
Struct stat f_stat;

If (STAT (filename, & f_stat) =-1 ){
Return-1;
}

Return (long) f_stat.st_size;
}
The following is an example:
-----------------------------------------------------------------
# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/STAT. h>

Long
Get_file_size (char * filename)
{
Struct stat f_stat;

If (STAT (filename, & f_stat) =-1 ){
Return-1;
}

Return (long) f_stat.st_size;
}


Int main (INT argc, char * argv [])
{
If (argc = 1)
{
Printf ("% s size: % d/N", argv [0], get_file_size (argv [0]);
Printf ("Usage: [% s] [filename]/n", argv [0]);
}

Else
Printf ("% s size: % d", argv [1], get_file_size (argv [1]);
System ("pause ");
Return 0;
}

4.
Using the filelength (INT) function is simple.
# Include <Io. h>
# Include <fcntl. h>
Int main (INT argc, char * argv [])
{
Int nfp = open ("./test.txt", o_rdonly );
Int Len = filelength (NFP );

/*
// Or
File * FP;
Fp = fopen ("./test.txt", "R ");
Int nfp = fileno (FP); // convert the file descriptor into a handle
Int Len = filelength (NFP );
*/

}

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.