读书人

怎样得知文件的大小,该怎么处理

发布时间: 2012-03-06 20:47:55 作者: rapoo

怎样得知文件的大小
知道一个文件的名字,如何知道文件的大小,在不打开文件的前提下

[解决办法]
CFile::GetLength
[解决办法]

/*man fstat*/
#if 0
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *file_name, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *file_name, struct stat *buf);

DESCRIPTION
These functions return information about the specified file. You do
not need any access rights to the file to get this information but you
need search rights to all directories named in the path leading to the
file.

stat stats the file pointed to by file_name and fills in buf.

lstat is identical to stat, except in the case of a symbolic link,
where the link itself is stat-ed, not the file that it refers to.

fstat is identical to stat, only the open file pointed to by filedes
(as returned by open(2)) is stat-ed in place of file_name.

struct stat {
dev_t st_dev; /* device */
ino_t st_ino; /* inode */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device type (if inode device) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */
};

#endif

/*Sample*/
#include <stdio.h>
#include <sys/stat.h>
int main(void)
{
const char file[]= "a.txt ";
struct stat f_s={0};
int f_size=0;
stat(file,&f_s);
f_size=f_s.st_size;

printf( "%s file size=%d \n ",file,f_size);
return 0;
}
[解决办法]
还是用

OpenFile

GetFileSize

好些

读书人网 >C语言

热点推荐