
long GetFileLength(FILE fileptr) {
if (fileptr == NULL) return 0; /fileptr=NULL是赋值语句,应为==/
long fOffset = ftell(fileptr);
if (fseek(fileptr, 0, SEEK_END)<0) return 0; /fseek函数失败时返回负值/
long file_size = ftell(fileptr);
fseek(fileptr, fOffset, SEEK_SET);
return file_size;
}
1 如何得到文件大小
方法有好几个 但有的受 *** 作系统限制 此处提供一个比较通用的:
long GetFileSize(char filename)
{
long siz = 0;
FILE fp = fopen(filename, "rb");
if (fp) {
fseek(fp, 0, SEEK_END);
siz = ftell(fp);
fclose(fp);
}
return siz;
}
2 如何删除文件
方法也有好几个 但有的受 *** 作系统限制 此处提供一个比较通用的:
unlink(filename);
用以下的方法可以获取一个文件的字节数:
先用fopen打开文件,然后把文件指针指向文件尾
再用ftell获得文件指针当前位置(即文件长度)
源代码:
#include "stdafxh"
#include <stdioh>
#include <iostream>
using namespace std;
int main()
{
FILE fp = NULL;
int nFileLen = 0;
fp = fopen("c:/Testtxt", "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
1MFC中的方法:(C++)
CFileStatus status;
CFile::GetStatus("D:\\testtxt",status);
long lSizeOfFile;
lSizeOfFile = statusm_size;
lSizeOfFile的值就是D:\\testtxt文件的大小
2标准C获得文件大小的5种方法
(注意:"__FILE__"指的是当前文件,你可以改为有效路径的目标文件,比如"D:\\testtxt")
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 "stdafxh"
#include "stdioh"
#include <sys/stath>
#include <ioh>
#include <FCNTLH>
int getfilesize()
{
int iresult;
struct _stat buf;
iresult = _stat(__FILE__,&buf);
if(iresult == 0)
{
return bufst_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;
}
我测试了你的代码:
#include<stdioh>#include<stringh>
#include<stdlibh>
void main()
{
FILE p=fopen("d:\\jiuibftxt","rt");
int length = 0;
for(;fgetc(p)!=EOF;length++);
fclose(p);
printf("第一种方式,文件长度=%d\n",length);
p=fopen("d:\\jiuibftxt","rb");
fseek(p,0,2);
length=ftell(p);
fclose(p);
printf("第二种方式,文件长度=%d\n",length);
}
文本文件的内容是:
FILE p=("jiuibftxt","rt");
int length;
for(;fgetc(p)!=EOF;length);
FILE p=(jiuibftxt","rb");
int length;
fseek(p,0,2);
length=ftell(p);
程序的输出是:
原因分析:
在windows下,以文本方式写入文件的\n会被转换为\r\n(也就是0x0D0A),输出的时候,\r\n会被转换回\n。
fgetc在读入时会将\r\n转换成一个\n;上面的文本文件有6个回车换行。
所以第一种方式比第二种方式少6
C语言获得文件的长度方式就是第二种:
FILEfp;fp=fopen("localfile","rb");// localfile文件名
fseek(fp,0,SEEK_SET);
fseek(fp,0,SEEK_END);
long longBytes=ftell(fp);// longBytes就是文件的长度
#include<stdioh>
#include<stdlibh>
void main()
{
FILEfp;
int a;
if((fp=fopen("1txt","rb"))==NULL)
{
printf("此文件无法打开");
exit(0);
}
fseek(fp,0,2);
a=ftell(fp);
printf("%d\n",a);
fclose(fp);
}
望采纳!
1。GetLength()
不行,貌似它获取的是缓冲区内的数据大小,最大8192字节。
2。Seek()
不行,经他人验证不行,我也就懒得试了。
3。QueryOption()
获取的居然是乱码。
ULONG file_total_size=0;
TCHAR szContentLength[64]={0};
ULONG dwInfoSize = 64;
file->QueryOption(>
C语言获取文件长度及全部内容,参考代码如下:
FILEfp;
fp=fopen("localfile","rb");// localfile文件名
fseek(fp,0L,SEEK_END); / 定位到文件末尾 /
flen=ftell(fp); / 得到文件大小 /
p=(char )malloc(flen+1); / 根据文件大小动态分配内存空间 /
if(p==NULL)
{
fclose(fp);
return 0;
}
fseek(fp,0L,SEEK_SET); / 定位到文件开头 /
fread(p,flen,1,fp); / 一次性读取全部文件内容 /
p[flen]=0; / 字符串结束标志 /
all:strchangeo
gcc -o strchangeo -c strchangec
strchangeo:strchangec
gcc -o strchange strchangeo
clean:
rm -rf strchange o
以上就是关于C语言获得文件大小时ftell始终返回-1全部的内容,包括:C语言获得文件大小时ftell始终返回-1、请问C语言如何得到文件大小,如何删除文件、c代码中如何获取一个文件的字节数~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)