Calculates the length of a binary file.

View versions (1)

Interface

#include <codecogs/computing/io/binary/file_length.h>

using namespace Computing::Io::Binary;

Returns the length of a file in bytes. Useful when you need to dynamically allocate memory to hold the contents of a file you intend to read. This function was designed to replicated the similarly named filelength function that comes with some distributions of C/C++, particularly on DOS platforms. There doesn't appear to be an equivalent in Unix.

Example 1

#include <stdio.h>
#include <codecogs/computing/io/binary/file_length.h>

using namespace Computing::IO::Binary;

int main ()
{
  FILE* stream = fopen("/codecogs/io/binary/file_length.h","rb");
  printf("\n File length=%d", fileLength(stream));
  fclose(stream);

  return 0;
}

Parameters

stream
A pointer to the file stream

Returns

The length of the file in bytes. Return -1 if an error is encountered.
GPL Licence — free for non commercial use. See Licence details.