O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code Sample 8.2: File Size

long int GetFileSize( FILE * hFile )
{
  long int lFileSize, lFilePos;
  if (hFile == NULL)
  {
     return -1;
  }

  lFilePos = ftell(hFile); // Store current filepos
  fseek(hFile, 0, 2);      // Go to the end of the file
  lFileSize = ftell(hFile);// Get final position

  fseek(hFile, lFilePos, 0); // Restore filepos

  return lFileSize;
}

In order to process character-level I/O, there are two groups of functions for processing individual characters and strings terminated by a carriage return. The most commonly used character-by-character functions are:

fgetc ( <handle> )Gets the next character in the file
fputc ( <handle>, <character> )Puts a character to the file
getchar()Gets a character from stdin
putchar( <character> )Writes a character to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required