十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
用以下的方法可以獲取一個(gè)文件的字節(jié)數(shù):
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的東至網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
先用fopen打開文件,然后把文件指針指向文件尾.
再用ftell獲得文件指針當(dāng)前位置(即文件長度).
源代碼:
#include "stdafx.h"
#include stdio.h
#include iostream
using namespace std;
int main()
{
FILE* fp = NULL;
int nFileLen = 0;
fp = fopen("c:/Test.txt", "rb");
if (fp == NULL)
{
cout "can't open file" endl;
return 0;
}
fseek(fp,0,SEEK_END); //定位到文件末
nFileLen = ftell(fp); //文件長度
cout "file len = " nFileLen endl;
return 0;
}
可以用 stat (win 下 _stat)函數(shù)直接得文件尺寸。
man 2 stat
1.MFC中的方法:(C++)
CFileStatus status;
CFile::GetStatus("D:\\test.txt",status);
long lSizeOfFile;
lSizeOfFile = status.m_size;
lSizeOfFile的值就是D:\\test.txt文件的大小
2.標(biāo)準(zhǔn)C獲得文件大小的5種方法
(注意:"__FILE__"指的是當(dāng)前文件,你可以改為有效路徑的目標(biāo)文件,比如"D:\\test.txt")
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 */
}
#include "stdafx.h"
#include "stdio.h"
#include sys/stat.h
#include io.h
#include FCNTL.H
int getfilesize()
{
int iresult;
struct _stat buf;
iresult = _stat(__FILE__,buf);
if(iresult == 0)
{
return buf.st_size;
}
return NULL;
}
int getfilesize01()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _filelength(fp);
//return NULL;
}
int getfilesize02()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}
int getfilesize03()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}
int getfilesize04()
{
FILE *fp;
if((fp=fopen(__FILE__,"r"))==NULL)
return 0;
fseek(fp,0,SEEK_END);
return ftell(fp); //return NULL;
}
int getfilesize05()
{
FILE *fp;
char str[1];
if((fp=fopen(__FILE__,"rb"))==NULL)
return 0;
for(int i = 0;!feof(fp);i++)
{
fread(str,1,1,fp);
}
return i - 1; //return NULL;
}
int main(int argc, char* argv[])
{
printf("getfilesize()=%d\n",getfilesize());
printf("getfilesize01()=%d\n",getfilesize01());
printf("getfilesize02()=%d\n",getfilesize02());
printf("getfilesize03()=%d\n",getfilesize03());
printf("getfilesize04()=%d\n",getfilesize04());
printf("getfilesize05()=%d\n",getfilesize05());
return 0;
}
int fseek(
FILE *stream,
long offset,
int origin
);
stream 文件指針.offset 從第三個(gè)參數(shù)origin開始計(jì)算的偏移字節(jié)數(shù)origin 初始位置必須為STDIO.H中定義的以下值之一: SEEK_CUR 文件指針的當(dāng)前位置.SEEK_END 文件末尾.SEEK_SET 文件開頭./SPAN/dd如果成功, fseek 返回0. 否則返回非零值. 使用這個(gè)函數(shù)定位后,你就可以取到你想要位置的字節(jié)了。以上是我從MSDN上翻譯過來的。
#include stdio.h
// 如果在s中找到ch,則返回第一個(gè)ch的索引,否則返回-1
int HasByte(char s[], char ch) {
int i;
for(i = 0; s[i]; ++i)
if(s[i] == ch) return i;
return -1;
}
// 返回s中出現(xiàn)ch的次數(shù)
int Times(char s[], char ch) {
int i,count = 0;
for(i = 0; s[i]; ++i)
if(s[i] == ch) ++count;
return count;
}