굳이 c언어에서 파일의 길이를 알아내려면
fseek(file,0,SEEK_END);
len = ftell(file);
rewind(file);
명령어의 조합으로 알아 낼수 있지만.. 느린거 같기도 하고. 그래서 다른 방법을 찾아 보았더니
linux에서는 stat 라는 함수가 있는데, 이것을 이용하면 파일의 상태를 얻어 올 수 있다.
char filename[];
struct stat statinfo;
stat(filename, &statinfo);
len = statinfo.st_size;
그리고 fstat로도 해봤는데
fp = fopen();
fstat(fp,&statinfo);
으로는 안되었다. 아무래도 타입이 다르거나 아니면 FILE 구조체의 다른 변수를 사용해야 할듯 하다.
이미 열어 놓은 파일이라면 fstat로 하는게 훨씬 빠르게 작동이 가능할 듯 하다.
#include <sys/stat.h>
int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
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 ID (if special file) */
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 status change */
};
[출처 : http://linux.die.net/man/2/stat]
|