-
You don't declare and give value for "length". This is why you get error C2065. For example:
int main()
{
// This causes error C2065, because compiler doesn't know
// identifier "x".
int i = 100 * x;
}
To solve problem:
int main()
{
// Here we declare (tell compiler that identifier "x" means integer value) and
// define (tell compiler that identifier "x" value is 1) identifier "x"
int x = 1;
// Now this doesn't cause any problems, because compiler a) knows identifier x and
// b) there is assigned value for identifier "x" (actually C++ compiler doesn't know
// this, and forgetting to give actuall value for identifier "x" would cause random // runtime behaviour a.k.a. bugs)
int i = 100 * x;
}
Here is one possible way to calculate length.
LONGLONG done = 0;
UINT size = BUFFERSIZE;
BYTE buffer[BUFFERSIZE];
int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR);
if (theFile != -1)
{
// Getting file length and storing it to length value.
//
// SEEK_END sets position to the end of file.
//
// _lseek returns the offset in a 64-bit integer, in bytes, of the new position
// from the beginning of the file (in this case it returns length of file,
// because we set seek position to END OF FILE).
//
long length = _lseeki64( theFile, 0L, SEEK_END );
if( length == -1L )
{
// The function returns –1L to indicate an error and sets errno.
// Avoid file processing, but close file
size = 0L;
// Specific error handlers
switch( errno )
{
case EBADF:
// File descriptor is invalid
// TODO: ADD EXTRA ERROR HANDLING
// For example: throw exception or show message box for user.
break;
case EINVAL:
// Value for origin is invalid or the position
// specified by offset is before the beginning of the file.
// TODO: ADD EXTRA ERROR HANDLING
// For example: throw exception or show message box for user.
break;
default:
// Unknown error code
// TODO: ADD EXTRA ERROR HANDLING
// For example: throw exception or show message box for user.
break;
}
}
if( length <= 0 )
{
// On devices incapable of
// seeking (such as terminals and printers), the return value
// is undefined.
length = 1L;
}
// Move file reading position to beginning of the file.
_lseeki64 (theFile, 0L, SEEK_SET);
while (size == BUFFERSIZE)
{
s