■■得到文件字节大小,我这种方法行不行?但有错误?怎么改呀?■■

smartdraw 2002-05-31 02:46:25
int fgetfize(AnsiString asdir,AnsiString asfilename)
{
FILE* fp;
int fsize;

fp=fopen(asdir+"\\"+asfilename,"w");
fsize=fp->bsize;
return fsize;
}
提示错误:
[C++ Error] UDFile.cpp(509): E2034 Cannot convert 'AnsiString' to 'const char *'
[C++ Error] UDFile.cpp(509): E2342 Type mismatch in parameter '__path' (wanted 'const char *', got 'AnsiString')
我应当怎么写?
...全文
57 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kang哥 2002-06-05
  • 打赏
  • 举报
回复
int handle;
long filesize;
handle = open("1.txt",O_RDONLY);
filesize=filelength(handle);
apache_cd 2002-06-02
  • 打赏
  • 举报
回复
教你一个简单的方法
int FileSize(AnsiString File){
TFileStream *file=new TFileStream(File,fmOpenRead);
return file->Size;
file->Free();
}
zjqc 2002-06-01
  • 打赏
  • 举报
回复
是的!FILESEEK函数是很典型的!不要想太多了!以上是教材版本!同意!
tiger_qi 2002-05-31
  • 打赏
  • 举报
回复

int iFileHandle;
int iFileLength;//文件长度
int iBytesRead;
char *pszBuffer;
iFileHandle = FileOpen("keyword.ini",fmOpenRead);
iFileLength = FileSeek(iFileHandle,0,2);
// 获得文件长度
FileSeek(iFileHandle,0,0);
teatool 2002-05-31
  • 打赏
  • 举报
回复
TSearchRec sr;
int filesize;
FILE *FShatter;
if (FindFirst(FileName,faAnyFile,sr)==0)
{
filesize=sr.Size;
int Attrs = FileGetAttr(FileName);
if (Attrs & faReadOnly)
FileSetAttr(FileName, Attrs & !faReadOnly);
}
else
return;
Pro2->Max=filesize;
Pro2->Position=0;


这可是我程序里的一个代码片段呀,你要改一下才能用,,
teatool 2002-05-31
  • 打赏
  • 举报
回复
我在程序里也需要得到文件大小,我总是用FindFirst来找这个文件,然后....
jishiping 2002-05-31
  • 打赏
  • 举报
回复
不好意思哪,我记错了。C++Builder里没有FileSize这个函数。用stat()就可以了。
#include <sys\stat.h>
struct stat sb;
stat(((asdir+"\\"+asfilename).c_str(), &sb);
sb.st_size 就是文件的大小,sb.st_mtime 就是文件的修改时间。
tiger_qi 2002-05-31
  • 打赏
  • 举报
回复
文件格式中好像第几个字节是代表文件大小来,我忘记了,你可以直接读那个代表字节大小的东西读出来就可以了
jsp说的最简单了;)
jishiping 2002-05-31
  • 打赏
  • 举报
回复
fp=fopen(asdir.c_str()+"\\"+asfilename.c_str(),"w");
应该是
fp=fopen((asdir+"\\"+asfilename).c_str(),"w");

给你一个最简单的函数,
FileSize(asdir+"\\"+asfilename); 就可以了。
我不懂电脑 2002-05-31
  • 打赏
  • 举报
回复
#include <stdio.h>

long filesize(FILE *stream);

int main(void)
{
FILE *stream;

stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
fclose(stream);
return 0;
}

long filesize(FILE *stream)
{
long curpos, length;

curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;

}
smartdraw 2002-05-31
  • 打赏
  • 举报
回复
fp=fopen(asdir.c_str()+"\\"+asfilename.c_str(),"w");
这样也错了,系统提示错误:

[C++ Error] UDFile.cpp(509): E2085 Invalid pointer addition
[C++ Error] UDFile.cpp(511): E2034 Cannot convert 'int' to 'void *'
mygodness 2002-05-31
  • 打赏
  • 举报
回复
bsize?有没有这个东西呀,有点把我弄糊涂了!
mygodness 2002-05-31
  • 打赏
  • 举报
回复
错了:
fp=fopen(asdir.c_str()+"\\"+asfilename,"w");
mygodness 2002-05-31
  • 打赏
  • 举报
回复
fp=fopen(asdir.c_str+"\\"+asfilename,"w");

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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