1.用SHELL来取得
-
- #ifdef _LINUX
- sprintf(cmd, "cd %s;find . -name \"*.*
- \"|xargs ls -l|awk '{size+=$5}END{print size}'",
- 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. fseek头再fseek尾相减
- 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 );
- fseek( file_ptr, 0L, SEEK_SET );
- PosBegin = ftell( file_ptr );
- fseek( file_ptr, 0L, SEEK_END );
- PosEnd = ftell( file_ptr );
- fseek( file_ptr, PosCur, SEEK_SET );
-
- return PosEnd - PosBegin;
- }
3. 利用stat()来取得
- long get_file_size( char * filename )
- {
- struct stat f_stat;
-
- if( stat( filename, &f_stat ) == -1 ){
- return -1;
- }
-
- return (long)f_stat.st_size;
- }
下面是一个例子:
- #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.利用函数filelength(int)很简单即可得到
- #include <io.h>
- #include <fcntl.h>
- int main(int argc, char *argv[])
- {
- int nFp = open("./test.txt", O_RDONLY);
- int len = filelength(nFp);
-
-
-
-
-
-
-
-
- }
(bat603) |