这个代码用来计算文件长度有什么问题啊为什么会是输出-1呢?
#include
long filesize( FILE *fp)
{
long cur_pos,length;
cur_pos = ftell(fp);
fseek(fp,0,SEEK_END);
length=ftell(fp);
fseek(fp,cur_pos,SEEK_SET);
return length;
}
void main(int argc, char *argv[])
{
FILE *fp;
char Name[20];
fp=fopen("test.txt","rb");
fgets(Name,20,fp);
printf("file length is %ld.\n",filesize(fp));
fgets(Name,20,fp);
fclose(fp);
}
这个应该是默认的吧,像find函数,我改了一下
#include <stdlib.h>
#include <stdio.h>
long filesize( FILE *fp)
{
long cur_pos,length;
cur_pos = ftell(fp);
fseek(fp,0,SEEK_END);
length=ftell(fp);
fseek(fp,cur_pos,SEEK_SET);
return length;
}
int main(int argc, char *argv[])
{
FILE *fp;
char Name[20];
fp=fopen("test.txt","r");
fgets(Name,20,fp);
long long n=filesize(fp);
if(n==-1)
{
n=0;
}
printf("file length is %ld.",n);
fgets(Name,20,fp);
fclose(fp);
}
文件打开成功了吗?