急~~~请教,c语言中有没有那个函数可以计算一个文件的长度的?

mimi_mouse 2002-05-20 06:00:31
谢谢,或者如何判断一个文件为空呢?
...全文
385 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
fang_jb 2002-05-23
  • 打赏
  • 举报
回复
stat函数,一个就可以
newmem 2002-05-23
  • 打赏
  • 举报
回复
同意c中用fseek()和ftell();手册中有说明这两个函数的配套用法,
和上面的差不多
zhanghk 2002-05-21
  • 打赏
  • 举报
回复
filelength
elli 2002-05-21
  • 打赏
  • 举报
回复
The stat and fstat calls conform to SVr4, SVID, POSIX, X/OPEN, BSD 4.3.
Windows也支持,所以可移植算可以的了。
shornmao 2002-05-21
  • 打赏
  • 举报
回复
stat是系统相关的,可能不具备可移植性。
mozhanshi 2002-05-21
  • 打赏
  • 举报
回复
如果你的文件名是知道的就简单了,比如文件为: c:\autoexec.bat,则:


filelength(open("c:\\autoexec.bat", O_RDONLY));
注:cui(蚊子王)教给我的。
wangxb 2002-05-21
  • 打赏
  • 举报
回复
同意用上面的方法。
用stat函数可以返回所引用的文件的特性,然后判断函数的返回值即可。
当然还有fstat()和lstat()等。
elli 2002-05-20
  • 打赏
  • 举报
回复
判断一下stat的返回值。
mimi_mouse 2002-05-20
  • 打赏
  • 举报
回复
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

main()
{
struct stat sbuff;
FILE *fp;

fp=fopen("file","rb");
stat(fp,&sbuff);
printf( "File size: %d\n", sbuff.st_size );
}
为什么输出的是134513267?我的文件明明是空的~~
fei1703 2002-05-20
  • 打赏
  • 举报
回复
ftell <stdio.h>


long ftell( FILE *stream );
Parameters
stream
Target FILE structure
Libraries
All versions of the C run-time libraries.

Return Values
ftell returns the current file position. The value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return–linefeed translation. Use ftell with fseek to return to file locations correctly. On error, ftell returns –1L. On devices incapable of seeking (such as terminals and printers), or when stream does not refer to an open file, the return value is undefined.

Remarks
The ftell function gets the current position of the file pointer (if any) associated with stream. The position is expressed as an offset relative to the beginning of the stream.

Note that when a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. For example, if a file is opened for an append and the last operation was a read, the file position is the point where the next read operation would start, not where the next write would start. (When a file is opened for appending, the file position is moved to end of file before any write operation.) If no I/O operation has yet occurred on a file opened for appending, the file position is the beginning of the file.

In text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using ftell and fseek to move within a file that ends with a CTRL+Z may cause ftell to behave improperly near the end of the file.
fei1703 2002-05-20
  • 打赏
  • 举报
回复
int fseek( FILE *stream, long offset, int origin );
Parameters
stream
Pointer to FILE structure
offset
Number of bytes from origin
origin
Initial position
mimi_mouse 2002-05-20
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>

main()
{
FILE *fp;
long fsizes;

fp=fopen("file","rb");
fseek(fp,0,2);
fsizes=ftell(fp);
printf("file size= %s",fsizes);
}

他干吗说我segmentation fault(core dumped)????
heimeng 2002-05-20
  • 打赏
  • 举报
回复
filelength()
terron 2002-05-20
  • 打赏
  • 举报
回复
查msdn!!
lbaby 2002-05-20
  • 打赏
  • 举报
回复

fseek(src,0,2);
fsizes=ftell(src);/*得出文件大小*/
fseek的第二,三个参数是何意
ftell是何涵数?系统的?

你自己去查手册吧
zzkoo 2002-05-20
  • 打赏
  • 举报
回复
fseek(src,0,2);
fsizes=ftell(src);/*得出文件大小*/
fseek的第二,三个参数是何意
ftell是何涵数?系统的?
elli 2002-05-20
  • 打赏
  • 举报
回复
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

void main( void )
{
struct _stat buf;
int result;
char buffer[] = "A line to output";

/* Get data associated with "stat.c": */
result = _stat( "stat.c", &buf );

/* Check if statistics are valid: */
if( result != 0 )
perror( "Problem getting information" );
else
{
/* Output some of the statistics: */
printf( "File size : %ld\n", buf.st_size );
printf( "Drive : %c:\n", buf.st_dev + 'A' );
printf( "Time modified : %s", ctime( &buf.st_atime ) );
}
}
lw549 2002-05-20
  • 打赏
  • 举报
回复
同意 lbaby(永不停息)的观点。
我的问题就是这样解决的。
lbaby 2002-05-20
  • 打赏
  • 举报
回复
不好意思,
错了
是:
FILE* src;
lbaby 2002-05-20
  • 打赏
  • 举报
回复
回复人: hiee_ylf(小马哥) :
int i=0;
char chr;
chr=fgetc(fp);
while(chr!=EOF)
{ i++;
chr=fgetc(fp);
}
这个我以前也用过
太慢了

另外int i有可能溢出

c中可以这样:
FILE src;
long fsizes;
......


fseek(src,0,2);
fsizes=ftell(src);/*得出文件大小*/


加载更多回复(3)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧