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 );
*/
}