how to get file size in c++

I need to write a dll which include a function that return file size


would any one help me to finish the part between /*** and **/

/***
bool Check_file_size ()
{
int Size_1 = GetSize("myself.dll") ;
int Size_2 = GetSize("upper_folder\serial.txt") ;
int Size_3 = GetSize("upper_folder\happy.fcv") ;

if (Szie_1 != default_1 ) return(false);
if (Size_2 != default_2 ) return(false);
if (Size_3 != default_3 ) return(false);
}

int Get_Size(string path);
{
....
....
return(Size);
}

**/

let's give an example of what upper_folder means

as above mentioned, there is a "myself.dll" in C:\Programs File\Success\libraries\

then the "serial.txt" and "happy.fcv" is in C:\Programs File\Success\


I am a beginner of programming ^^

big thanks
Last edited on
fopen();
fseek(seek to the end of the file)
ftell(get the file size in bytes)
fseek(again come to the begining of the file if you want to traverse the file)
On Windows, you can also use the GetFileSizeEx() or GetFileSize() function ( http://www.google.com/search?btnI=1&q=msdn+GetFileSizeEx ).

You'll have to have an open HANDLE to the file to use it.

Hope this helps.
Windows/*NIX (and probably most conventional OS') support stat function.
Oh, zap, how did I forget that one?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main( int argc, char** argv )
  {
  if (argc != 2)
    {
    cerr << "usage:\n"
            "  filesize FILENAME\n\n";
    return 1;
    }

  struct stat filestatus;
  stat( argv[ 1 ], &filestatus );

  cout << filestatus.st_size << " bytes\n";

  return 0;
  }

See http://linux.die.net/man/2/stat

;->
hehehehe... me too... :P

infact replied to someone about stat...
Duoas
Oh, zap, how did I forget that one?
#include <iostream>
using namespace std;

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main( int argc, char** argv )
{
if (argc != 2)
{
cerr << "usage:\n"
" filesize FILENAME\n\n";
return 1;
}

struct stat filestatus;
stat( argv[ 1 ], &filestatus );

cout << filestatus.st_size << " bytes\n";

return 0;
}

See http://linux.die.net/man/2/stat

;->


thanks all of the people giving help
but do this compatible on windows xp,vista?
thanks again
Yes.
Duoas
On Windows, you can also use the GetFileSizeEx() or GetFileSize() function ( http://www.google.com/search?btnI=1&q=msdn+GetFileSizeEx ).

You'll have to have an open HANDLE to the file to use it.


how the program know which file i want to get ? filename ??
i have find the answer on internet already

handle=FileOpen("filename", FILE_CSV|FILE_WRITE, ';');
if(handle>0)
{
FileWrite(handle, Close[0], Open[0], High[0], Low[0], TimeToStr(orderOpen));
FileClose(handle);
}

i don't know what is handle before..
Last edited on
below site say that when handle = 0, then it means no error , it opposite to when i have learn in the ticket = XXX

http://msdn.microsoft.com/en-us/library/z5hh6ee9(VS.80).aspx

just soliloquize, don't need to reply anything, thanks :)
if a file is reading by another process, and i want to get the size of this file, which is better for getting it's size? GetFileSize or Ftell ?
int Get_Size( string path )
{
// #include <fstream>
FILE *pFile = NULL;

// get the file stream
fopen_s( &pFile, path.c_str(), "rb" );

// set the file pointer to end of file
fseek( pFile, 0, SEEK_END );

// get the file size
int Size = ftell( pFile );

// return the file pointer to begin of file if you want to read it
// rewind( pFile );

// close stream and release buffer
fclose( pFile );

return Size;
}
It would be better to use off_t (or loff_t) than int.
off_t (or loff_t) than int.

off_t Size = ftell( pFile );

right?
Topic archived. No new replies allowed.