Function ftell () in C

The function long int ftell (FILE * stream) in standard C Library returns the current file location of the given Stream.

Declare ftell () function in C

Below is the declaration for ftell () function in C:

 long int ftell ( FILE * stream ) 

Parameters

stream - This is the pointer to a FILE object that identifies the Stream.

Returns the value

This function returns the current value of position indicator. If an error occurs, -1 is returned, and the global variable errno is set to a positive value.

For example

The following program C illustrates the usage of the ftell () function in C:

 #include int main () { FILE * fp ; int len ; fp = fopen ( "baitapc.txt" , "r" ); if ( fp == NULL ) { perror ( "Xay ra loi khi mo file!!!" ); return (- 1 ); } fseek ( fp , 0 , SEEK_END ); len = ftell ( fp ); fclose ( fp ); printf ( "Tong kich co cua baitapc.txt = %d bytesn" , len ); return ( 0 ); } 

Suppose we have the bait.txt file with the following content:

 Hoc C co ban va nang cao tai QTM !!! 

Compiling and executing the above program will produce the following result if the file has the above content, otherwise it will give different results depending on the content of the file.

Function ftell () in C Picture 1

According to Tutorialspoint

Previous post: Function fsetpos () in C

Next lesson: Function fwrite () in C

4 ★ | 5 Vote

May be interested

  • Function fscanf () in CFunction fscanf () in C
    the int fscanf function (file * stream, const char * format, ...) in library c reads the input formatted from a stream.
  • Function fprintf () in CFunction fprintf () in C
    the int fprintf function (file * stream, const char * format, ...) in standard c library sends formatted output to a stream.
  • The sprintf () function in CThe sprintf () function in C
    the int sprintf function (char * str, const char * format, ...) in the standard c library sends the formatted output to a string str.
  • Remove () function in CRemove () function in C
    the int remove (const char * filename) function in library c standard deletes the filename so it is no longer accessible.
  • Function setbuf () in CFunction setbuf () in C
    the function void setbuf (file * stream, char * buffer) in the standard c library defines how a stream is buffered. this function should be called once the file attached to the stream has been opened, but before any input or output operation has taken place.
  • Function tmpfile () in CFunction tmpfile () in C
    function file * tmpfile (void) in standard c library create temporary files in wb + mode. the temporary file created will be automatically deleted when the stream is closed (fclose function) or when the program ends.
  • The function sscanf () in CThe function sscanf () in C
    the function int sscanf (const char * str, const char * format, ...) in library c standard reads the input formatted from a string.
  • Function fputc () in CFunction fputc () in C
    the function int fputc (int char, file * stream) in library c writes one character (an unsigned char) defined by the char parameter to the given stream and increases the position indicator for the stream.
  • Function fwrite () in CFunction fwrite () in C
    function size_t fwrite (const void * ptr, size_t size, size_t nmemb, file * stream) in standard c library writes data from the array pointed by ptr to the given stream.
  • Function fgetc () in CFunction fgetc () in C
    the function int fgetc (file * stream) in c library standard takes the next character (an unsigned char) from the given stream and increases the position indicator for that stream.