Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get File size? 1

Status
Not open for further replies.

inetd

Technical User
Jan 23, 2002
115
HK
How to get the size in byte of a file?
 
Use fstat

See help Pages

hnd
hasso55@yahoo.com

 
Also look at FileGetAttr.
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
//---------------------------------------------------------------------------
long __fastcall GetFileSize(const AnsiString FileName)
{
TSearchRec SearchRec;
long returnValue=-1;

if(FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec)==0)
returnValue=SearchRec.Size;
FindClose(SearchRec);

return returnValue;

}
//---------------------------------------------------------------------------

You can use a int64 type variable instead of a long to support files that are gigabytes in size.
 
This is how I do it.

hFileHandle = (void*)FileOpen("D:\\File.txt", fmOpenRead );
iFileSize = GetFileSize(hFileHandle, NULL);
FileClose((int)hFileHandle);
 
I had this program so I just copied
long FileSize (FILE *stream)
{
long length, curpos;
fseek (stream, 0L, SEEK_END);
length = ftell(stream);
fseek (stream, 0L, SEEK_SET);
return length;
}
//----------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *handle;
AnsiString Nombre = FileListBox1->FileName;
AnsiString Tamanio;
handle = fopen(Nombre.c_str(), "r");
Tamanio = AnsiString (FileSize (handle));
Nombre = StrCat (Tamanio.c_str(), " Bytes");
Label1->Caption = Nombre.c_str();
fclose(handle);
}
I hope it help you
 
A lot of different methods, but mind that in some cases the file can be located on a network. Without read rights it is not possible to open the file.
In those cases you will need FileGetAttr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top