常见的有char、int、long、short、float、double及指针等.
字符类型
这里单只char,char变量在内存中存储的是字符对应的ASCII码值。所以长度也是固定的,为1个字节。
整数类型
查了资料发现对各个类型的整数占用字节数是有一个规定的,虽然16位32位或者64位的机子各种类型所占用的长度并不一样。规则如下:
short 至少占用2个字节。
int 建议为一个机器字长。32位环境下机器字长为4字节,64位环境下机器字长为8字节。
short 的长度不能大于 int,long 的长度不能小于 int。
所以说,short并不一定短,long并不一定短,有可能都和int一样长。
浮点数(小数)
C中浮点数有两种,float和double,不像整数,小数的长度始终是固定的,float占用4个字节,double占用8个字节。
指针
C中指针即为地址,所以编辑器的寻址空间则是指针的地址范围。所以指针变量在32位机器的寻址空间为2^32,要将所有地址都表示出来则需要为4个字节长度,所以要占用4个字节长度。同理64位的编译器则需要8个字节长度。
用以下的方法可以获取一个文件的字节数:先用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
}
这个要看字符数是什么数据类型,可以用sizeof(类型) *** 作符来测试字节数。1、英文字母:如果是char 型,那么是占用1个字节,8位。如果是string型,应该是两个字节,16位,因为末尾还有个\0字符。
比如:
char c = 'a'//它占用一个字符
char c[] = "a"//占用两个。
char c[] = "abcde"//占用4+1 = 5 个,结尾有一个'\0'
2、数字:有很多种类,从小到大分别为:
short、int、float、long、double
字节为:1个、2个、4个、8个、8个
位数为:8位、16位、32位、64位、64位
3、汉字:理论上说,一个汉字占用两个字节,16位。比如:中国,就占用4个字节,例如:
char c[] = "中国"//占用4+1 = 5个字符
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)