当前位置:首页 » 编程语言 » c语言文件的大小

c语言文件的大小

发布时间: 2022-07-11 22:32:10

c语言 计算文件大小fgetpos,数据出现负数且不正确

在目前的C/C++标准中,ftell为long型的,也就是32位的,它的范围就是-2,147,483,648到2,147,483,647
,你要对超过2G的文件操作,必须用类似gcc中
Function:
off64_t
ftello64
(FILE
*stream)
这样的函数
或可用系统的SDK
DWORD
GetFileSize(

HANDLE
hFile,
//
handle
of
file
to
get
size
of

LPDWORD
lpFileSizeHigh
//
address
of
high-order
word
for
file
size

);

❷ 用C语言怎样测试一个文本文件的大小

#include <io.h>
#include <stdio.h>

int main(void)
{
FILE * f = 0; int fn = 0, sz = 0;
f = fopen("c:\\abc.txt", "rb");
if(!f) {
fprintf(stderr, "File Not Found.\n");
return 0;
}/*end if*/
fn = _fileno(f); /*取得文件指针的底层流式文件号*/
sz = _filelength(fn);/*根据文件号取得文件大小*/
printf("文件abc.txt大小为:%d字节.\n", sz);
fclose(f);
return 0;
}/*end main*/

备注:上述程序适用于任何一种文件,都可以直接求出大小。

❸ c语言如何通过文件属性获取文件大小知道

c语言可以通过stat()函数获得文件属性,通过返回的文件属性,从中获取文件大小。
#include
<sys/stat.h>
可见以下结构体和函数
struct
stat
{

_dev_t
st_dev;

_ino_t
st_ino;

unsigned
short
st_mode;

short
st_nlink;

short
st_uid;

short
st_gid;

_dev_t
st_rdev;

_off_t
st_size;

//文件大小

time_t
st_atime;

time_t
st_mtime;

time_t
st_ctime;

};
stat(const
char
*,
struct
_stat
*);
//根据文件名得到文件属性
参考代码:
#include <sys/stat.h>
void main( )
{
struct stat buf ;
if ( stat( "test.txt", &buf ) < 0 )
{
perror( "stat" );
return ;
}
printf("file size:%d\n", buf.st_size );
}

❹ C语言使用标准完成给定的源代码存储程序,找出文件的大小

long length =0;
if((fp = fopen (argv[1],"rb"))==NULL){
printf( "Usage: fsize <file>" ) ;
exit(1) ;}
fseek(fp,0L,SEEK_END);
length=ftell(fp);
fclose( fp ) ;
printf ( " File %s size : %d in " , argv[1] , length ) ;
return 0 ;

❺ C语言中,怎么知道文件的大小

你需要使用fseek函数将文件指针指向最后:
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
其中sz就是文件的大小。
当你知道文件的大小后,就可以通过fseek重新定义到文件开始的位置:
fseek(fp, 0L, SEEK_SET);
或使用下面的函数定义的文件开始:
rewind(fp);
可以到秒秒学上看看相关的内容,上面的课程讲解得可以的。

❻ C语言能读写多大的文件

使用fopen和fseek的时候,在32位操作系统中,没办法操作2G以上的文件,可以通过open64、lseek解决这个问题

❼ c语言如何计算文件大小

#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE*fp;
int a;
if((fp=fopen("1.txt","rb"))==NULL)
{
printf("此文件无法打开");
exit(0);
}
fseek(fp,0,2);
a=ftell(fp);
printf("%d\n",a);
fclose(fp);
}
望采纳!

❽ C语言获取txt文件大小两种方法的差异

我测试了你的代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
voidmain()
{
FILE*p=fopen("d:\jiuibf.txt","rt");
intlength=0;
for(;fgetc(p)!=EOF;length++);
fclose(p);
printf("第一种方式,文件长度=%d ",length);
p=fopen("d:\jiuibf.txt","rb");
fseek(p,0,2);
length=ftell(p);
fclose(p);
printf("第二种方式,文件长度=%d ",length);
}

文本文件的内容是:

FILE *p=("jiuibf.txt","rt");

int length;

for(;fgetc(p)!=EOF;length);

FILE *p=(jiuibf.txt","rb");

int length;

fseek(p,0,2);

length=ftell(p);

程序的输出是:

FILE*fp;
fp=fopen("localfile","rb");//localfile文件名
fseek(fp,0,SEEK_SET);
fseek(fp,0,SEEK_END);
longlongBytes=ftell(fp);//longBytes就是文件的长度

❾ C语言建立文件有没有大小限制啥的

从函数本身来说是没有任何限制的,受限制的只是系统和保存的分区的文件格式等外部条件的。

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:233
java驼峰 发布:2025-02-02 09:13:26 浏览:651
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:532
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726