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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 GB Limit 2

Status
Not open for further replies.

AEtherson

Programmer
Oct 10, 2003
119
GB
Hi All,

Just a quick question. I have a data file that is over 2GB and I need a file size reading.


below is my current code


Meth1
Code:
unsigned long  GetFileLength ( FILE * fileName)
{
     unsigned long pos = ftell(fileName);
     unsigned long len = 0;
   
     fseek ( fileName, 0L, SEEK_END );
     len = ftell ( fileName );
     fseek ( fileName, pos, SEEK_SET );
     return len;


}

Meth2
Code:
unsigned long  GetFileLength2 ( char * fileName)

{
	struct stat buf;
	int i = stat ( fileName, &buf );

	if (i !=0)
	   MessageBox(NULL,"ERROR for STAT","ERROR",0);

	return buf.st_size;
}
both methords below return - numbers when the files get over 2GB, any help is appreciated
 
take a look at:


see hwo they do it.. i realize that you use fstream and that getfilesize() won't really work for you, but it might give you some ideas on how to store exceptionally large file sizes.

what are the numbers you get when its over 2GB? you might have to several long ints to hold the size.. you can add them together (not really add, but you can split up the sum of the file size- when you are done dealing with the number of bytes in the first split, move to the next number). its hard to say much more without knowing more details of what you're doing.
 
please be a little more specific about what actually happens when you run the code. what are the numbers you get, does it crash, what exactly happens
 
GetFileSizeEx() is a proper function but it's a hard task to obtain a file handle in a standard C++ filestream-oriented code. Now (as usually) C/C++ file size type is too small. Use double and incapsulate platform-dependent code in a function (in separate cpp file), for example:
Code:
/// For Windows:
#include <windows.h>
double dblFileSize(const char* fname)
{
  if (!fname && !*fname)
     return 0.0;
  HANDLE h;
  WIN32_FIND_DATA info;

  if ((h=FindFirstFile(fname,&info)) 
      != INVALID_HANDLE_VALUE)
  {
     FindClose(h);
     if ((info.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
         == 0) // Is it a file?
     {
        union
        {
          struct { DWORD low, high; } lh;
          __int64 size; // MS large int extension
        } file;
        file.lh.low = info.nFileSizeLow;
        file.lh.high= info.nFileSizeHigh;
        return file.size; // will be casted to double
     }
     // It's a directory, not a file
  }
  return 0.0; // No such name.
}
You may return -1.0 on errors (if you wish)...
 
Because of I don't like to expose platform language extensions in mainstream sources (strictly speaking, I hate names started with more than one underscores;). The double type is a world-wide standardized thing and sufficient for huge file size (by range and precision;)...
 
yeah.. i know that.. but my suggestion was to just look at how they deal with large file sizes and try to mimic it somehow. i didn't know how to do it using fstream.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top