资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言怎么查字节函数 c语言中怎么知道字符所占字节数

c代码中如何获取一个文件的字节数~~

用以下的方法可以获取一个文件的字节数:

成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的东至网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

先用fopen打开文件,然后把文件指针指向文件尾.

再用ftell获得文件指针当前位置(即文件长度).

源代码:

#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)函数直接得文件尺寸。

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.标准C获得文件大小的5种方法

(注意:"__FILE__"指的是当前文件,你可以改为有效路径的目标文件,比如"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;

}

C语言如何读文件指定的字节,有哪些库函数

int fseek(

FILE *stream,

long offset,

int origin

);

stream 文件指针.offset 从第三个参数origin开始计算的偏移字节数origin 初始位置必须为STDIO.H中定义的以下值之一: SEEK_CUR 文件指针的当前位置.SEEK_END 文件末尾.SEEK_SET 文件开头./SPAN/dd如果成功, fseek 返回0. 否则返回非零值. 使用这个函数定位后,你就可以取到你想要位置的字节了。以上是我从MSDN上翻译过来的。

用c语言涉及一段程序来搜索给定的字节(byte)?急!!!!

#include stdio.h

// 如果在s中找到ch,则返回第一个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中出现ch的次数

int Times(char s[], char ch) {

int i,count = 0;

for(i = 0; s[i]; ++i)

if(s[i] == ch) ++count;

return count;

}


文章名称:c语言怎么查字节函数 c语言中怎么知道字符所占字节数
当前路径:http://cdkjz.cn/article/ddggcge.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220