ftell - C stdio.h

C examples for stdio.h:ftell

Type

function

From


<cstdio>
<stdio.h>

Description

Get current position in stream

Prototype

long int ftell ( FILE * stream );

Parameters

ParameterDescription
stream FILE object

Return Value

On success, the current position is returned.

On failure, -1L is returned, and errno is set to a system-specific positive value.

Example

This program prints out the size of main.cpp in bytes.

Demo Code


#include <stdio.h>

int main ()/*from ww  w  .  j  a  v  a2  s  .  co  m*/
{
  FILE * pFile;
  long size;

  pFile = fopen ("main.cpp","rb");
  if (pFile==NULL){
     perror ("Error opening file");
     return -1;
  }

  fseek (pFile, 0, SEEK_END);   // non-portable
  size=ftell (pFile);
  fclose (pFile);
  printf ("Size of main.cpp: %ld bytes.\n",size);

  return 0;
}

Related Tutorials